JUnit 4、HSQLDB_2.2.9、DBUnit_2.4.8を使用してテストを行う際に問題が発生しました。私は次の入力ファイル(input.xml)を消費しています:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<record_type record_type="Logical" />
<record_type record_type="Conceptual" />
</dataset>
次のようにgetDataSetメソッドを使用してこのファイルを使用しています。
@Override
protected IDataSet getDataSet() throws Exception
{
System.out.println("Getting data set...");
loadedDataSet = new FlatXmlDataSetBuilder();
FlatXmlDataSet dataSet = loadedDataSet.build(new File("test\\test\\files\\input.xml"));
System.out.println("Dataset is "+dataSet.getTable("record_type").getValue(0, "record_type"));
return dataSet;
}
これで、私のSetUpメソッドは次のことを行います。
protected void setUp() throws Exception
{
System.out.println("Setting up environment...");
getSetUpOperation();
tearDown();
createTables();
}
createTablesメソッドは次のとおりです。
private void createTables() throws SQLException, Exception
{
System.out.println("Creating database tables");
String createRecordTypeTableSQL = "CREATE TABLE IF NOT EXISTS record_type "+
"(record_type VARCHAR(30) NOT NULL,"+
"PRIMARY KEY (record_type))";
PreparedStatement createErrorTypeTablePS = getConnection().getConnection().prepareStatement(createRecordTypeTableSQL);
createRecordTypeTablePS.executeUpdate();
}
私のget接続は次のとおりです。
@Override
protected IDatabaseConnection getConnection() throws Exception
{
Connection jdbcConnection = null;
Class.forName("org.hsqldb.jdbc.JDBCDriver");
String url = "jdbc:hsqldb:sample";
Properties props = new Properties();
props.setProperty("user","sa");
props.setProperty("password","");
jdbcConnection = DriverManager.getConnection(url, props);
return new DatabaseConnection(jdbcConnection);
}
私のテストケースは次のとおりです。
@Test
public void testSelect() throws Exception
{
String selectSQL = "SELECT * from record_type";
PreparedStatement selectPS =
getConnection().getConnection().prepareStatement(selectSQL);
ResultSet resultRS = selectPS.executeQuery();
String recordType=null;
if(resultRS.next())
{
recordType = resultRS.getString("record_type");
System.out.println("Found record type: "+recordType);
}
assertEquals("Result ","Logical",recordType);
}
したがって、私が期待しているのは「論理」という値ですが、得られる結果は「null」です。getDataSetメソッドがinput.xmlファイルを正しく消費しているかどうかを検証するために、次の行を追加しました。
System.out.println("Dataset is "+dataSet.getTable("record_type").getValue(0, "record_type"));
これは、期待どおりにrecord_type"Logical"を返します。したがって、私の問題は、getDataSetメソッドによって返される返されたIDataSetが、取得したデータを作成したデータベーステーブルにバインドしていないことだと思います。
注:record_typeをデータベースに手動で挿入しようとしましたが、テストメソッドによって正常に挿入および取得されたため、データベース接続にも問題はありません。
何か案は?
更新: 私の問題を修正した唯一のことは、getDataSet()メソッドに以下を追加することでした:
DatabaseOperation.CLEAN_INSERT.execute(getConnection(), dataSet);
この追加のコード行がなくても、これを実行できたと確信しています。私がしていることは何か問題がありますか?