背景
使用SpringBoot开发,Junit测试时要加载所有的配置,启动速度相当缓慢,每测一次就相当于重启一次服务,太难受了。于是就在考虑有没有简单快速的加载方式。搜索了一下,发现了简友的优雅单测系列方法:优雅单测-4如何优雅的做Mybatis单测。测试了一下可行,就继续优化了一下,为备忘,做一下记录。
上代码
说不多说,先上代码。
配置类TestConfig
@Configuration
@MapperScan(basePackages = {"com.xxx.**.mapper"})
@PropertySource(value = "classpath:/application-test.yml", factory = TestConfig.ResourceFactory.class)
@Import({DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
public class TestConfig {
// 服务用到的Mybatis,自动注入
@Autowired
private LogMapper logMapper;
// 要测试的服务,生成Bean
@Bean
public LogService logService() {
return new LogServiceImpl(logMapper);
}
// 读取YML配置文件
static class ResourceFactory extends DefaultPropertySourceFactory {
@Override
public org.springframework.core.env.PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = (name == null) ? resource.getResource().getFilename() : name;
assert sourceName != null;
if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
Properties properties = factory.getObject();
assert properties != null;
return new PropertiesPropertySource(sourceName, properties);
}
return super.createPropertySource(name, resource);
}
}
}
配置文件application-test.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
check-config-location: true
configuration:
map-underscore-to-camel-case: true
global-config:
db-config:
id-type: auto
logic-delete-field: deleted
logic-delete-value: 1
logic-not-delete-value: 0
mapper-locations: classpath*:mapper/*.xml
测试类
基本跟之前的测试类写法一样,只需要加上:@ContextConfiguration(classes = {TestConfig.class})
即可。
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
public class DBTest {
@Autowired
private LogService logService;
@Test
public void test() {
QueryWrapper<Log> wrapper = Wrappers.query();
wrapper.lambda().lt(Log::getCreateTime, DateUtils.addDays(new Date(), -2))
.orderByDesc(Log::getCreateTime).last("limit 1");
Log log = logService.getOne(wrapper);
System.out.println(log == null ? "null" : log.toString());
}
}
说明
- 添加
@Configuration
不多说 - 添加扫描Mapper的配置
@MapperScan(basePackages = {"com.xxx.**.mapper"})
- 读取YML的配置文件
@PropertySource(value = "classpath:/application-test.yml", factory = TestConfig.ResourceFactory.class)
,为支持yml格式,添加了ResourceFactory。 - 使用自带的数据库及MyBatisPlus配置类,觉得单体测试没必要使用连接池,加载DataSource及MybatisPlus支持
@Import({DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
- 之后可以使用
@Autowired
装配所有Mapper了 - 手动实例化使用到的类
@Bean
- 也可使用
@ComponentScan
来自动装配,不过相互依赖过多,容易出问题 - 添加支持YML的方法
static class ResourceFactory extends DefaultPropertySourceFactory
- 配置文件中定义需要的配置,如数据库连接,Mybatis配置
- 测试类上添加自定义配置
@ContextConfiguration(classes = {TestConfig.class})
完工