事务性 DbUnit 测试中的拆卸期间出现异常

Exception during teardown in a Transactional DbUnit test

测试class声明

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestH2Config.class })
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DbUnitTestExecutionListener.class,
        TransactionalTestExecutionListener.class
})
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class BookServiceIT {
    // ... dependencies injection goes here (omitted for brevity)
    @Test
    @DatabaseSetup("/datasets/import.xml")
    public void testCase1() throws Exception {
        // .. create Book in Organization(id=2)
        // ... read and create data in database (omitted for brevity)
    }
}

test/resources/datasets/import.xml

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <Organization id="2" name="Gov. Library" inactive="false" />
</dataset>

用于运行测试的Maven命令:

mvn verify -T 1C

我正在附加异常堆栈跟踪(测试仅在 Codeship Linux 环境中失败):

WARNING: Caught exception while allowing TestExecutionListener [com.github.springtestdbunit.TransactionDbUnitTestExecutionListener@3f191a96] to process 'before' execution of test method [public void com.test.integration.BookServiceIT.testCase1() throws java.lang.Exception] for test instance [com.test.integration.BookServiceIT@15f18e5] org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_JNCMPQJWFCJEXX62EA1P06VLP: ITEST.BOOK FOREIGN KEY(LIBRARY_ID) REFERENCES ITEST.ORGANIZATION(ID) (2)"; SQL statement:delete from ORGANIZATION [23503-195]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
        at org.h2.message.DbException.get(DbException.java:179)
        at org.h2.message.DbException.get(DbException.java:155)
        at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:425)
        at org.h2.constraint.ConstraintReferential.checkRowRefTable(ConstraintReferential.java:442)
        at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:317)
        at org.h2.table.Table.fireConstraints(Table.java:976)
        at org.h2.table.Table.fireAfterRow(Table.java:994)
        at org.h2.command.dml.Delete.update(Delete.java:101)
        at org.h2.command.CommandContainer.update(CommandContainer.java:101)
        at org.h2.command.Command.executeUpdate(Command.java:260)
        at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:193)
        at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:165)
        at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
        at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
        at org.dbunit.database.statement.SimpleStatement.executeBatch(SimpleStatement.java:69)
        at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:126)
        at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
        at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:162)
        at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:60)
        at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:160)
        at com.github.springtestdbunit.TestExecutionListenerChain.call(TestExecutionListenerChain.java:93)
        at com.github.springtestdbunit.TestExecutionListenerChain.runChain(TestExecutionListenerChain.java:126)
        at com.github.springtestdbunit.TestExecutionListenerChain.forwards(TestExecutionListenerChain.java:115)
        at com.github.springtestdbunit.TestExecutionListenerChain.beforeTestMethod(TestExecutionListenerChain.java:91)
        at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:249)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:70)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
        at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

我尝试使用其他执行侦听器(我在 DbUnit documentation 中找到),但我遇到了同样的异常...

@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionDbUnitTestExecutionListener.class
})

更新:我试图添加一个明确的拆解:在 testCase() 方法的末尾删除了 Book。但是异常仍然存在。

它在你的堆栈跟踪中说:

org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_JNCMPQJWFCJEXX62EA1P06VLP: ITEST.BOOK FOREIGN KEY(LIBRARY_ID) REFERENCES ITEST.ORGANIZATION(ID) (2)"; SQL statement:delete from ORGANIZATION [23503-195]

因此当 BOOK table 中仍有记录引用 ORGANIZATION [=22] 时,您要从 table ORGANIZATION 中删除=] 通过 FK_JNCMPQJWFCJEXX62EA1P06VLP 外键。

你们有破解数据库的特殊代码吗?如果没有,您可能需要明确地编写它,或者如果您的业务要求允许,则将您的外键定义为 ON DELETE CASCADE

我通过指定 type = DatabaseOperation.REFRESH 更改了 DbUnit 的行为。现在它不会在测试后清理数据库并且没有 JDBC 异常。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestH2Config.class })
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionDbUnitTestExecutionListener.class
})
@Transactional
public class BookServiceIT {
    // ... dependencies injection goes here (omitted for brevity)
    @Test
    @DatabaseSetup(value = "/datasets/import.xml", type = DatabaseOperation.REFRESH)
    public void testCase1() throws Exception {
        // .. create Book in Organization(id=2)
        // ... read and create data in database (omitted for brevity)
        // .. delete the Book
    }
}

请参阅 @DatabaseSetup 文档。

dbUnit 按照数据集文件中列出的数据的相反顺序删除行。确保文件的外键行位于关系的主要 keys/one 侧之后。

更新: 测试中存在一本书 table 行,所以告诉 dbUnit 也将其删除。一种方法是将外键 table 的空 table 添加到数据集文件 after 主 table:

<book/>

所以 dbUnit 拆卸操作看到 table 并且也将其清理。