SpringBoot2单元测试数据源指定和数据定制初始化

本文实现以下目的: 1. 定制单元测试数据源,与集成单元测试的数据源分开; 2. 每个单元测试均重新构建数据表和测试数据;

本文实现以下目的:
1. 定制单元测试数据源,与集成单元测试的数据源分开;
2. 每个单元测试均重新构建数据表和测试数据;


测试配置

@TestConfiguration
@MapperScan(basePackages = "your.package.dao",
sqlSessionFactoryRef = "testSqlSessionFactory")
public class TestConfig {
    @Autowired
    private Environment env;
    @Bean(name = “testDataSource”)
    public DataSource testDataSource() {
        // 通过配置初始化数据源。
        DruidDataSource defaultDS = new DruidDataSource();
        defaultDS.setUrl(env.getProperty(“spring.datasource.unittest.url”));
        defaultDS.setUsername(env.getProperty(“spring.datasource.unittest.username”));
        defaultDS.setPassword(env.getProperty(“spring.datasource.unittest.password”));
        defaultDS.setDriverClassName(env.getProperty(“spring.datasource.unittest.driver-class-name”));
        return defaultDS;
    }
    @Bean(name = “testSqlSessionFactory”)
    public SqlSessionFactory testSqlSessionFactory(@Qualifier(“testDataSource”) DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath*:mapper/*.xml”));
        return bean.getObject();
    }
    @Bean(name = “testTransactionManager”)
    public DataSourceTransactionManager testTransactionManager(@Qualifier(“testDataSource”) DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = “testSqlSessionTemplate”)
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier(“testSqlSessionFactory”) SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class CompanyDaoTest {
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    @Qualifier(“testDataSource”)
    private DataSource dataSource;
    @Before
    public void setUp() {
        // 初始化测试表结构和数据。
        ClassPathResource initSql = new ClassPathResource(“script/mysql/company_init.sql”);
        ClassPathResource dataSql = new ClassPathResource(“script/mysql/company_data.sql”);
        DataSourceInitializer dsi = new DataSourceInitializer();
        dsi.setDataSource(dataSource);
        dsi.setDatabasePopulator(new ResourceDatabasePopulator(true, true, “utf-8”, initSql, dataSql));
        dsi.setEnabled(true);
        dsi.afterPropertiesSet();
    }
    @Test
    public void test_insertCompany() {
        Company com = new Company();
        int row = companyMapper.insertCompany(com);
        Assert.assertEquals(“添加公司失败。”, 1, row);
        Assert.assertTrue(“未设置companyId”, com.getCompanyId() > 0);
        Company com2 = companyMapper.selectCompanyById(com.getCompanyId());
        Assert.assertNotNull(“读取对象失败。”, com2);
    }
}
文章出处:JPCode.net 精品代码网
链接:http://www.jpcode.net/article/10009

标签: SpringBoot


评论:
billyzh
we34rt 回复
billyzh
uiiuiui 回复
billyzh
23423424 回复
billyzh
Re:erer
billyzh
Re:3443434
billyzh
dfdfdf 回复
billyzh
uuuuuu 回复