junit/spring-test/spring-test-dbunit/dbunit集成数据库单元测试

谷梁子濯
2023-12-01

DAL层的测试核心目标:

1、表结构是否是预期;

2、SQL逻辑是否正确;

3、各类配置、代码是否有遗漏、且是否符合预期;

各技术框架选取原因

Junit:不用说了,标准的测试框架;

spring-test:用spring的IOC容器环境,以及各种框架和spring集成的便利;

DBunit:数据库数据透明化,查询数据不受数据库数据影响;

spring-test-dbunit:提供dbunit注解方式,把相关的资源注入spring ioc容器中。很有意思的一个东西,见github:https://github.com/springtestdbunit/spring-test-dbunit

各技术框架版本&Maven库情况

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>
<dependency>  
  <groupId>com.github.springtestdbunit</groupId>  
  <artifactId>spring-test-dbunit</artifactId>  
  <version>1.0.1</version>  
  <scope>test</scope>  
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.1.3.RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.dbunit</groupId>
    <artifactId>dbunit</artifactId>
    <version>2.4.9</version>
    <scope>test</scope>
</dependency>

TestCase实例代码:

 

package com.xxx.dal.dao.mysql;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.ExpectedDatabase;
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
import com.xxx.biz.enums.PointComputeType;
import com.xxx.biz.utils.DateUtil;
import com.xxx.dal.dao.KpiScoreConfigDAO;
import com.xxx.dal.dataobjects.KpiScoreConfigDO;

/**
 * KpiScoreConfigDAOImplTest.class Description:
 * 
 * @author simple56 2013-6-20
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/spring/secp-mysql.xml","classpath:spring/dbunit.xml"})
@Transactional
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionDbUnitTestExecutionListener.class })
public class KpiScoreConfigDAOImplTest {
    @Resource
    private KpiScoreConfigDAO kpiScoreConfigDAO;
    
    @Test
    @DatabaseSetup("sec_dp_kpi_score_config.xml")
    public void queryAllNodesBy(){
        List<KpiScoreConfigDO> result=kpiScoreConfigDAO.queryAllNodesBy("2013-04-01 00:00:00", "2013-08-01 00:00:00");
        Assert.assertEquals(2, result.size());
    }
    
    @Test
    @DatabaseSetup("sec_dp_kpi_score_config.xml")
    @ExpectedDatabase(assertionMode=DatabaseAssertionMode.NON_STRICT,value="sec_dp_kpi_score_config_insert_expect.xml")
    public void insert(){
        Date c=new Date();
        KpiScoreConfigDO kpiScoreConfigDO=new KpiScoreConfigDO();
        kpiScoreConfigDO.setNodeId("01003001");
        kpiScoreConfigDO.setPointComputeType(PointComputeType.Asc.name());
        kpiScoreConfigDO.setScore(new BigDecimal(3.5));
        kpiScoreConfigDO.setStartTime(DateUtil.getCurrentQ1StartTime(c));
        kpiScoreConfigDO.setValue(new BigDecimal(50.0));
        kpiScoreConfigDAO.insert(kpiScoreConfigDO);
        
        Date c2=new Date();
        KpiScoreConfigDO kpiScoreConfigDO2=new KpiScoreConfigDO();
        kpiScoreConfigDO2.setNodeId("01003001");
        kpiScoreConfigDO2.setPointComputeType(PointComputeType.Asc.name());
        kpiScoreConfigDO2.setScore(new BigDecimal(5.0));
        kpiScoreConfigDO2.setStartTime(DateUtil.getCurrentQ1StartTime(c2));
        kpiScoreConfigDO2.setValue(new BigDecimal(90.0));
        kpiScoreConfigDAO.insert(kpiScoreConfigDO2);
    }
    
}

 

最后我想说:

这里集成了各测试框架常用的用法,其中最大的特点在于使用了很多注解,不再是以前的各种对TestCase父类的继承,让测试过程爽起来!尤其是spring-test-dbunit,让dbunit的使用也迈向注解时代,good!

 

 

 

 

转载于:https://www.cnblogs.com/yingfeng56/p/3358753.html

 类似资料: