2

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 errorJunit 行に が表示されます : databaseTester.onSetup();DBUnitHelper.getConnection()デバッガーでこれを実行すると、が呼び出されていないことが わかります。

Oracle の制約を無効にするために何を修正する必要があるかについてのアイデアはありますか?

4

1 に答える 1

3

最初に尋ねるのは、「作業中のテーブルの制約はDEFERRABLEとして作成されましたか?」です。

これを確認するには、次のクエリを発行します。

SELECT constraint_name, table_name, DEFERRABLE 
  FROM all_constraints 
 WHERE owner = 'myschema'
   AND table_name = 'THE_TABLE';

制約がDEFERRABLEとして作成されていない場合、コマンドSET ALLCONSTRAINTSDEFERREDは基本的に効果がありません。

制約を延期できない場合は、それらを削除して延期可能として再作成する必要があります。延期できるように変更することはできません。

于 2011-01-21T23:35:00.377 に答える