<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
String[] activeProfiles = applicationContext.getEnvironment().getActiveProfiles();
if (ArrayUtil.contains(activeProfiles, "test")) {
// 单元测试,使用 h2 数据库,防止 mysql 中存在垃圾数据
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
} else {
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
}
return interceptor;
}
spring:
datasource:
url: jdbc:h2:mem:unit_test
username: admin
password: 123456
driver-class-name: org.h2.Driver
# h2 页面控制台,可以选择性添加
h2:
console:
enabled: true
path: /h2-console
@SpringBootTest
@ActiveProfiles({"test"})
@AutoConfigureMockMvc
@Slf4j
public class UserControllerTest {
// ......
}
maven test
进行测试。
default
),通过 postman 进行测试。