Oracle 10g および JUnit 4.5 で DbUnit 2.4.8 を使用しています。私が持っている DbUnit テストを実行している間、Oracle の外部キー制約を無効にして、他のすべてのテーブルから独立して単一のテーブルをテストできるようにしたいと思います。これは私がこれまでに持っているものです:
DatabaseTestCase を拡張するクラス (DBUnitHelper) を作成しました。私は
@Override
protected IDatabaseConnection getConnection() throws Exception {
if (usingInternalCtx_)
{
if (!esa.util.SysConfig.isRunning())
esa.util.SysConfig.startupSystem();
ctx_ = OraPool.getCtx();
}
//disable foreign keys
Connection con = ctx_.getConnection();
con.prepareStatement("SET CONSTRAINTS ALL DEFERRED").execute();
return new OracleConnection(con, "my_schema"); // DatabaseConnection(con_);
}
JUnit テスト メソッドは次のとおりです。
@Test
public void useDatabaseTesterToRemoveExistingDataThenRunTest()
{
IDataSet dataset = null;
try
{
IDatabaseTester databaseTester = dbunit_.getDatabaseTester();
databaseTester.setDataSet(dbunit_.getDataSet());
databaseTester.onSetup(); // clean out existing entries in the table specified by the dataset and populate it with entries from the database
IDataSet databaseDataSet = databaseTester.getDataSet();
// IDataSet databaseDataSet = con.createDataSet(); // uncomment to retrieve actual rows from the database
ITable actualTable = databaseDataSet.getTable(TABLE_NAME);
// Load expected data from an XML dataset
IDataSet expectedDataSet = dbunit_.getDataSet();
ITable expectedTable = expectedDataSet.getTable(TABLE_NAME);
// Assert new testing database table match expected (xml) table
assertEquals(3,expectedTable.getRowCount());
assertEquals(expectedTable.getRowCount(), actualTable.getRowCount());
assertEquals(expectedTable.getValue(1, "oid"), actualTable.getValue(1, "oid"));
Assertion.assertEquals(expectedTable, actualTable);
databaseTester.onTearDown(); // by default does nothing
} catch (Exception e)
{
e.printStackTrace();
fail("test_names has problems");
}
}
ORA-02291: integrity constraint violated - parent key not found error
Junit 行に が表示されます : databaseTester.onSetup();
。DBUnitHelper.getConnection()
デバッガーでこれを実行すると、が呼び出されていないことが わかります。
Oracle の制約を無効にするために何を修正する必要があるかについてのアイデアはありますか?