1.所需依赖(根目录下pom.xml)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-starter.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-test-autoconfigure -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>${mybatis-starter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-test-autoconfigure</artifactId>
<version>${mybatis-starter.version}</version>
</dependency>
2.所需依赖(infrastructure 即需要访问数据库的模块)
<!--单元测试使用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-test-autoconfigure</artifactId>
</dependency>
2.单元测试文件目录结构 infrastructure模块下
test
java
com.package.test
ApplicationTest.java
BaseTest.java
mapper
xxxMapperTest.java
resources
application.properties
application.properties 文件内含有数据库配置信息
ApplicationTest.java代码为启动代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.mybatis.spring.annotation.MapperScan;
//import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
//如果使用了 tk.mybatis 则import tk.mybatis的包
@MapperScan("com.package.*.mapper")
@ComponentScan(basePackages={"com.package"})
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class, args);
}
}
BaseTest.java 为我们写单元测试都需要用到的,只需要子类继承它就不需要每次都写注解了
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.test.context.junit4.SpringRunner;
//@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@MybatisTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class BaseTest {
@Test
public void test() {
}
}
XxxMapperTest.java 的写法
public class XxxMaperTest extends BaseTest {
@Autowired
private XxxMapper xxxMapper;
@Test
public void getIpAddress() {
XxxDO xxxDO = xxxMapper.getIpAddress(66777215L);
System.out.println(xxxDO);
Assert.assertNotNull(xxxDO);
}
}
我们现在可以直接单测某个mapper的某个方法了。
(215)