- 测试准备
- 测试类配置 - BusDeviceTestApp
- 测试类 - DeviceLogServiceTest
- Excel数据准备注意点
- 测试数据准备
- Maven 包导入
- Unitils额外配置(数据库连接)
- 参考
- 正文
- 测试整合之王Unitils(单元测试系列)
dev_equipment_info.xlsx - 测试数据
expected_dev_equipment_info.xlsx - 测试后数据
Excel数据准备注意点- 日期/时间格式 使用 'yyyy-mm-dd hh:mm:ss’格式
- 每个sheet表的首行为数据表字段名称
- sheet名称需要与表名完全相同,也可以使用database.tableName格式的sheet名
- 一个excel数据表可以同时有多个数据表
- Excel最后一行数据之后,不能只是清除内容,导入时无法识别
在pom.xml的dependencies中,增加
com.github.yangjianzhouspring-boot-unitils-starter1.3.0.RELEASEUnitils额外配置(数据库连接)
application-ut.properties 存放于resources下,其内容可参考
database.driverClassName=com.mysql.jdbc.Driver# 此数据库连接信息database.url=jdbc:mysql://127.0.0.1/septictank_device# 此数据库连接用户名database.userName=root# 此数据库连接用户密码database.password=123456# 此数据库连接的schemadatabase.schemaNames=septictank_device# 此数据库数据库类型:oracle/mysql/postgres等database.dialect=mysqldataSource.beanName=druidDataSource测试类配置 - BusDeviceTestApp
package com.citylink.device;import com.unitils.boot.autoconfigure.ConfigurableApplicationContextAware;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;@SpringBootApplication@ComponentScan(basePackages = {"com.citylink.device"}, basePackageClasses = {ConfigurableApplicationContextAware.class, DataSourcePostProcessorExt.class})public class BusDeviceTestApp { public static void main(String[] args) { SpringApplication.run(BusDeviceTestApp.class, args); }}
主要实现以下功能:
- basePackages = {“com.citylink.device”},扫描需要注入上下文的业务bean
- ConfigurableApplicationContextAware.class,为测试提供上下文,以便@SpringBean,@SpringBeanByName,@SpringBeanByType等从上下文中注入bean
- DataSourcePostProcessorExt.class,为替换系统中原有的dataSource bean的类,如测试时一直使用系统原数据库,很有可能是相应的dataSource未被正常替换
package com.citylink.device;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.stereotype.Component;import org.unitils.database.UnitilsDataSourceFactoryBean;import javax.sql.DataSource;@Componentpublic class DataSourcePostProcessorExt implements BeanPostProcessor { public DataSourcePostProcessorExt() { } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof DataSource) { try { return (new UnitilsDataSourceFactoryBean()).getObject(); } catch (Exception var4) { throw new RuntimeException("replace database throw exception ,can not continue to process", var4); } } else { return bean; } }}测试类 - DeviceLogServiceTest
package com.citylink.device;import com.citylink.device.pojo.Device;import com.citylink.device.service.IDeviceService;import com.citylink.security.common.msg.ObjectRestResponse;import com.unitils.boot.util.UnitilsBootBlockJUnit4Cla***unner;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.unitils.dbunit.annotation.DataSet;import org.unitils.dbunit.annotation.ExpectedDataSet;import org.unitils.spring.annotation.SpringBeanByType;import java.util.List;@RunWith(UnitilsBootBlockJUnit4Cla***unner.class)@SpringBootTest(classes = BusDeviceTestApp.class)@DataSet("dev_equipment_info.xlsx")public class DeviceLogServiceTest { @SpringBeanByType private IDeviceService deviceService; @Test @ExpectedDataSet("expected_dev_equipment_info.xlsx") public void testSelectAll() { ObjectRestResponse orr = deviceService.selectAll(); Assert.assertTrue(orr.isRel()); ListdeviceList = (List) orr.getData(); S����,����ystem.out.println("deviceList.size()=" + deviceList.size()); }}