给若依添加单元测试
方案一(简单)
方案二(异常困难但企业开发一般用这个)
在 activity 子模块中添加单元测试
S1.在 src 目录下创建 test.java.MapperTests 文件
S2.将以下内容复制进去
import com.ruoyi.activity.ActivityApplication;
import com.ruoyi.activity.domain.Activity;
import com.ruoyi.activity.mapper.ActivityMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.ArrayList;
import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest(classes = ActivityApplication.class)
@MapperScan(basePackages = "com.ruoyi.activity.mapper")
public class MapperTests {@Autowiredprivate ActivityMapper activityMapper;@Testpublic void testSelectActivityById() {Activity activity = activityMapper.selectActivityById(1);System.out.println(activity);}@Testpublic void testSelectActivityList() {List<Activity> activityList = activityMapper.selectActivityList(new Activity());System.out.println(activityList);}}
S3.将以下内容添加到 admin 的 pom.xml 中
<!-- Mysql驱动包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 阿里数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId></dependency><!-- SpringBoot Web容器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 测试所需 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency>
S4.在 ActivityMapper 前添加 Mapper 注解
S5.在 activity 包下创建 ActivityApplication
S6.添加如下内容
package com.ruoyi.activity;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;/*** 启动程序** @author qiheng*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class ActivityApplication
{public static void main(String[] args){// System.setProperty("spring.devtools.restart.enabled", "false");SpringApplication.run(ActivityApplication.class, args);System.out.println("(♥◠‿◠)ノ゙ 若依启动成功 ლ(´ڡ`ლ)゙ \n" +" .-------. ____ __ \n" +" | _ _ \\ \\ \\ / / \n" +" | ( ' ) | \\ _. / ' \n" +" |(_ o _) / _( )_ .' \n" +" | (_,_).' __ ___(_ o _)' \n" +" | |\\ \\ | || |(_,_)' \n" +" | | \\ `' /| `-' / \n" +" | | \\ / \\ / \n" +" ''-' `'-' `-..-' ");}
}
S7.分别从 admin 和 framework 模块中将相关配置文件复制过来
终于成功了!