基于springtestdbunit的Spring单元测试

在基于Spring开发测试的过程中,如果需要对DAO层进行单元测试,可以利用DBUnit来操作数据库,或者基于数据库数据进行测试验证。在Github上开源了一个DBUnit与Spring框架集成的项目,
https://github.com/springtestdbunit
为我们提供了基于注解的DBUnit操作。

springdbunit配置

为了能在Spring的测试中使用DBUnit,需要在测试用例类上做如下的注解,主要是增加 Spring TestExecutionListener.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringDbunitApplicationTest.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class, 
    DbUnitTestExecutionListener.class})
@DatabaseSetups({
    @DatabaseSetup(value = "classpath:company-dataset.xml", type = DatabaseOperation.CLEAN_INSERT),
    @DatabaseSetup(value = "classpath:employee-dataset.xml", type = DatabaseOperation.CLEAN_INSERT)
})
public class SpringDbunitApplicationTests {

Setup and TearDown

在DAO的单元测试中,通常的测试场景是,假定数据库处在某个指定状态(初始化),然后对数据库进行CRUD,并验证操作结果。在完成上述操作后,通过回滚来恢复数据库到操作前状态(重置)。springdbunit提供了两个非常有用的注解 @DatabaseSetup 和 @DatabaseTearDown 来进行数据库的数据初始化和重置。

初始化setup

假设使用 sampleDataSet.xml 作为数据文件,这个文件通常在测试类的同一个包下。
@DatabaseSetup("sampleDataSet.xml")

接下来,我们来看下这个注解的定义。

/**
 * Test annotation which indicates how to put a database into a know state before tests are run. This annotation can be
 * placed on a class or on methods. When placed on a class the setup is applied before each test methods is executed.
 *
 * @see DatabaseTearDown
 * @see ExpectedDatabase
 * @see DbUnitConfiguration
 * @see DbUnitTestExecutionListener
 *
 * @author Phillip Webb
 */
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Repeatable(DatabaseSetups.class)
public @interface DatabaseSetup {
    /**
     * Determines the type of {@link DatabaseOperation operation} that will be used to reset the database.
     * @return The type of operation used to reset the database
     */
    DatabaseOperation type() default DatabaseOperation.CLEAN_INSERT;
//略
}

从中我们可以了解到

  1. 这个DatabaseSetup 既可以注释在类上,又可以注释在方法上,与Junit的@BeforeClass和@Before起到类似的作用。
  2. 默认情况下,初始化的时候会进行 CLEAN_INSERT 操作,也就是说 DataSet.xml 中的数据在导入时,会先将所涉及的表的数据清空。
  3. DatabaseSetup可以有多个注解,并由@DatabaseSetups进行组织。具体请见本文的前述案例。

重置 teardown

@DatabaseTearDown 注解用来在测试完成之后重置数据库的上下文。与 @DatabaseSetup 类似,可以使用在类上或方法上,也可以进行嵌套,并指明相应的DBUnit数据操作模式。

断言 @ExpectedDatabase

软件测试的另外一个重要的问题就是断言。springtestdbunit同样提供了一个基于注释的断言。如下面的用例,

    @Test
    @DatabaseSetup("sampleData.xml")
    @ExpectedDatabase("expectedData.xml")
    public void testRemove() throws Exception {
        this.personService.remove(1);
    }

顾名思义,就是在测试用例执行完成后,断言数据库中相应的表中的数据与"expectedData.xml"中的一致。该断言与之前的两个有着相同的属性,可以在类或者方法上注解,还可以使用多个@ExpectedDatabase来组成复杂的断言。
在实际的测试中,可能还需要根据需要进行对断言的方式进行限定。
一般来说,数据库表的断言方式有:
1)包含,即预期结果是整个数据表的一部分,特别是新增时。
2)部分相同,即只比较预期和实际数据库表的部分列。某些如时间戳、ID等数据可能无法比较或者无需比较。
3) 数据相同,但数据的行排序不同。
4)数据完全相同。
以下是一个自定义的断言形式,
@ExpectedDatabase(assertionMode=DatabaseAssertionMode.NON_STRICT,value="expectedData.xml")

根据该工具的源码,可以了解到,

DatabaseAssertionMode.DEFAULT 

整库比较,类似前述比较场景中的4)

DatabaseAssertionMode.NON_STRICT  

类似前述比较场景中的2

     DatabaseAssertionMode.NON_STRICT_UNORDERED 

类似前述比较场景中的2+3
如果是场景1)的话,一般在@DatabaseSetup 会将数据库表清空,这样就可以转换成2/3/4的模式进行比较。另外一种方式是通过DAO 获取数据,在测试用例内部进行断言。

TODO:

@Transactional
@DataLoader

写在最后

本文案例来自:
https://github.com/hainet/spring-test-dbunit-sample

你可能感兴趣的:(基于springtestdbunit的Spring单元测试)