データベースのテストに DBUnit を使用しています。私のデータベースは空ではないので、既存の要素を無視して、テストによって挿入されたデータだけをテストしたいのです。
これは、テストの実行方法の例です。
1- テーブルには 10 個の要素が含まれています
2- DBUnit はデータセットからいくつかのデータを挿入します (3 要素)
3- 私のテスト挿入データ (1 要素)
4-期待されるデータセットには、最初のデータセットで定義された3つの要素と、テストによって最近追加された要素である4つの要素が含まれています
5-したがって、実際のテーブルと期待されるテーブルが等しいとアサートすると、エラーが表示されます。これは、テーブルに既に要素が含まれているため、正常です。
質問は: assert でデータベースに存在する要素を無視する方法はありますか? データセットとテストによって挿入されたデータをテストしたいだけです。
これはコードです:
@Override
protected IDataSet getDataSet() throws Exception {
// transforme fichier XML en BDD
URL url = this.getClass().getResource("/dataset-peqt2-init.xml");
File testFile = new File(url.getFile());
return new FlatXmlDataSetBuilder().build(testFile);
}
@Override
protected DatabaseOperation getSetUpOperation() throws Exception
{
return DatabaseOperation.REFRESH;
}
/**
* Reset the state of database
* Called before every test
*/
@Override
protected DatabaseOperation getTearDownOperation() throws Exception
{
return DatabaseOperation.DELETE;
}
/**
* get the actual table from the database
* @param tableName
* @return
* @throws Exception
* @throws SQLException
* @throws DataSetException
*/
private ITable getActualTable(String tableName) throws Exception, SQLException, DataSetException {
// get the actual table values
IDatabaseConnection connection = getConnection();
IDataSet databaseDataSet = connection.createDataSet();
return databaseDataSet.getTable(tableName);
}
/**
* get the expected table from the dataset
* @param tableName
* @param fileName
* @return
* @throws Exception
*/
private ITable getExpectedTable(String tableName, String fileName) throws Exception {
// get the expected table values
URL url = this.getClass().getResource("/"+fileName);
File testFile = new File(url.getFile());
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(testFile);
return expectedDataSet.getTable(tableName);
}
@Test
public void test01_insert() throws SQLException, Exception {
File file = new File(SynchroDerbi.class.getResource("/test-insert.lst").getFile());
log.debug("test01_insert() avec ref : "+file.getName());
SynchroDerbi.run(file);
String fileName = "dataset-insert-expected.xml";
actualTable = getActualTable("equipment");
expectedTable = getExpectedTable("equipment",fileName);
Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, new String[]{"id","idSite"});
}