<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 <bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean" />
</beans>

  4)测试数据的准备

  可以根据dbunit的xml格式准备测试数据,通过执行ExportData这个对象来导出现有测试库的数据,在命令行里面输入要导出的表名,即可把当前测试数据库的现有数据导出为xml

  测试的xml文件默认放到test/resources下和测试的代码相同的package中,比如test/resources/com/xxx/dao/下。

  5)单元测试用例的编写

  单元测试用例需要继承UnitilsJUnit3这个基类,顾名思义这个测试套件是依赖Junit3的。Unitils另外也提供了UnitilsJUnit4的基类。下面是我们的一个测试样例代码

  Java代码


public class AccountDaoTest extends UnitilsJUnit3 {

    @SpringApplicationContext({ "classpath:testapplication/appContext-common.xml",
            "classpath:testapplication/testDatasource.xml", "classpath:META-INF/spring/applicationContext-*.xml" })
    protected ApplicationContext applicationContext;

    @SpringBeanByType
    private AccountDao accountDao;

    @DataSet("ACCOUNT.xml")
    public void testGetAccount() {
        Account account = accountDao.getAccount("S31993k");
        System.out.println(JSON.toJSON(account));
        assertEquals(100, account.getBalance());
    }

    @DataSet("ACCOUNT.xml")
    public void testGetAccountNull() {
        Account account = accountDao.getAccount("23");
        assertEquals(null, account);
    }
    @DataSet("ACCOUNT.xml")
    @ExpectedDataSet("ACCOUNT_NEW.xml")
    public void testUpdateAccount() {
        accountDao.updateAccount("S31993k", 35);

    }

}