【问题标题】:dbunit disable Oracle constraintdbunit 禁用 Oracle 约束
【发布时间】:2011-01-21 22:43:48
【问题描述】:

我将 DbUnit 2.4.8 与 Oracle 10g 和 JUnit 4.5 一起使用。我想在运行我拥有的 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");
    }
}

我在 Junit 线上收到 ORA-02291: integrity constraint violated - parent key not found errordatabaseTester.onSetup();。当我使用调试器逐步执行此操作时,我看到 DBUnitHelper.getConnection() 从未被调用过。

关于我需要修复什么以禁用 Oracle 中的约束有什么想法吗?

【问题讨论】:

标签: oracle constraints dbunit


【解决方案1】:

首先要问的是:“您正在处理的表上的约束是否创建为 DEFERRABLE?”

您可以通过发出以下查询来检查:

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

如果没有将约束创建为 DEFERRABLE,则命令 SET ALL CONSTRAINTS DEFERRED 基本上没有效果。

如果约束不可延迟,您必须将它们删除并重新创建为可延迟的。您不能将它们修改为可延期。

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 2011-02-10
    • 1970-01-01
    • 2014-11-24
    相关资源
    最近更新 更多