springtestdbunit / spring-test-dbunit

Integration between the Spring testing framework and DBUnit
http://springtestdbunit.github.com/spring-test-dbunit/
Apache License 2.0
475 stars 238 forks source link

com.github.springtestdbunit.assertion.DatabaseAssertion throws Error instead of "promised" DatabaseUnitException when assertion fails! #97

Closed evys42 closed 8 years ago

evys42 commented 8 years ago

An attempt to use @ExpectedDatabase annotation brought unexpected results when assertion fails:

An offender seemed to be the org.dbunit.Assertion.assertEquals(...) methods, which throws java.lang.Error instead of promised org.dbunit.DatabaseUnitException when the assertion fails.

The assertion is invoked by

com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(...).

In a org.springframework.test.context.TestContextManager.afterTestMethod(...) CALL to the configured test execution listeners is, naturally, surrounded by try/catch block that intercepts java.lang.Exception. Unfortunately, when the listener throws a java.lang.Error instead, it escapes into surrounding org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate() method, preventing the "downstream" listeners from being executed. One of the skipped listeners is, obviously, a TransactionalTestExecutionListener, responsible for ending a current transaction.

Filed similar issues with:

An easy workaround would be to use a DbUnitTestExecutionListener "extention":

public class PeculiarDbUnitTestExecutionListener extends DbUnitTestExecutionListener {
    @Override
    public void afterTestMethod(TestContext testContext) throws Exception {
        try {
            super.afterTestMethod(testContext);
        } catch (Error e) {
            throw new DatabaseUnitException(e);
        }
    }
}
philwebb commented 8 years ago

It looks like this will be fixed in Spring 4.3 so I'd rather not put such hacks directly in DbUnitTestExecutionListener.