日期:2014-05-16  浏览次数:20433 次

Spring中集成dbunit进行单元测试

Dbunit的几种数据格式

?

其他:

xml的数据准备和维护太复杂

xls的格式什么的一点不对就报错

?

比较一下,还是CSV的比较靠谱

?

用在Spring里集成DbUnit写一测试基类,同时使用CSV作为准备数据源

?

?

/**
 * 测试基类.
 *
 * @author buming.pl
 */
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations={
	"classpath:/springbeans-data-source-test.xml",
	"classpath:/springbeans-rpt-ds-test.xml",
	"classpath:/ehcache.xml",
	"classpath:/springbeans-web-manager.xml",
	"classpath:/springbeans-dozer.xml",
	"classpath:/springbeans-hsf-remote.xml",
	"classpath:/springbeans-tbuic-client-test.xml",
	"classpath:/springbeans-web-bo.xml"
	})
@TransactionConfiguration(transactionManager="txManager",defaultRollback=true)
@Transactional
@ITestDataSet
public abstract class BaseTestCase extends AbstractTransactionalDataSourceSpringContextTests{

	/** The log. */
	protected final Log log = LogFactory.getLog(getClass());
	
	/** The data source. */
	@Resource
    private DataSource dataSource;  
    
    /** The database tester. */
    private DataSourceDatabaseTester databaseTester;
  
    /**
     * Inits the dbunit.
     *
     * @throws Exception the exception
     */
    @Before
    public void initDbunit() throws Exception {
        databaseTester = new DataSourceDatabaseTester(dataSource);
        databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
        databaseTester.setSchema("zuanshi_test");
        setUpCsvDataSet();
    }
    
    /**
     * Tear down database.
     *
     * @throws Exception the exception
     */
    @After
    public void tearDownDatabase() throws Exception {
      if (databaseTester != null && databaseTester.getDataSet()!=null) {
          databaseTester.onTearDown();
      }
    }
	
    /**
     * 设置CSV格式的初始化数据.
     *
     * @param file the new up csv data set
     * @throws Exception the exception
     */
    protected void setUpCsvDataSet(String file){
    	try {
    		IDataSet dataset = new CsvDataSet(new ClassPathResource(file).getFile());   ;
            databaseTester.setDataSet(dataset);
            databaseTester.onSetup();  
		} catch (Exception e) {
			e.printStackTrace();
		} 
    }
    
    /**
     * 设置CSV格式的初始化数据.
     *
     * @param file the new up csv data set
     * @throws Exception the exception
     */
    protected void setUpCsvDataSet() {
    	try {
    		File file = new File(this.getClass().getResource("").getPath()+"/csv");
    		if(file.exists()){
                IDataSet dataset = new CsvDataSet(file);
                databaseTester.setDataSet(dataset);
                databaseTester.onSetup();
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}

?

?

开始一直报错说:

org.dbunit.database.AmbiguousTableNameException

?

后来发现是其他的Schema下有相同的表名引起的,所以要制定Schema

?

?

databaseTester.setSchema("zuanshi_test");

?

?

使用的时候,只要在测试类下建立 ?csv 目录,在里面放入文件

?

?

table-ordering.txt

?

内容为表名的列表,一个表一行

?

其他就是以 ? ?表名.csv 的文件,一个表一个文件

?

?

例如:

?

memberid,contractver,createtime,updatetime,status,nickname

99999901,1,2011-08-24 11:11:00,2011-08-24 11:11:00,1,zuanshi001

99999902,1,2011-08-24 11:11:01,2011-08-24 11:11:01,1,zuanshi002

99999903,1,2011-08-24 11:11:02,2011-08-24 11:11:02,1,zuanshi003

99999904,1,2011-08-24 11:11:03,2011-08-24 11:11:03,1,zuanshi004

99999905,1,2011-08-24 11:11:04,2011-08-24 11:11:04,1,zuanshi005

99999906,1,2011-08-24 11:11:05,