dbunit 禁用 Oracle 约束
我将 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:违反完整性约束 - 未找到父键错误
:databaseTester.onSetup();
。当我使用调试器单步执行此操作时,我发现 DBUnitHelper.getConnection() 从未被调用。
关于我需要修复什么来禁用 Oracle 中的约束有什么想法吗?
I'm using DbUnit 2.4.8 with Oracle 10g and JUnit 4.5. I'd like to disable the foreign key constraints in Oracle while running the DbUnit tests I have so I can test single tables independent of all other tables. This is what I have so far:
I have created a class (DBUnitHelper) which extends DatabaseTestCase. I've
@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_);
}
The JUnit test method is:
@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");
}
}
I get an ORA-02291: integrity constraint violated - parent key not found error
on the Junit line: databaseTester.onSetup();
. When I step through this with the debugger, I see that the DBUnitHelper.getConnection()
is never being called.
Any ideas on what I need to fix to disable the constraints in Oracle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先要问的是:“您正在处理的表上的约束是否创建为 DEFERRABLE?”
您可以通过发出以下查询来检查这一点:
如果约束未创建为 DEFERRABLE,则命令 SET ALL CONSTRAINTS DEFERRED 本质上不起作用。
如果约束不可延迟,则必须删除它们并将其重新创建为可延迟。您无法将它们修改为可推迟。
The first thing to ask is: "Were the constraints on the tables you are working on created as DEFERRABLE?"
You can check this by issuing this query:
If the constraints are not created as DEFERRABLE, the command SET ALL CONSTRAINTS DEFERRED essentially has no effect.
If the constraints are not deferrable, you must drop and re-create them as deferrable. You can't modify them to be deferrable.