Annotation Interface DbUnitTestCase


@Target(FIELD) @Retention(RUNTIME) @Documented public @interface DbUnitTestCase
Marks a test class field, of a type implementing PrepAndExpectedTestCase, as the test case to drive: the extension configures and runs that instance instead of constructing one of its own, the "the object already exists and I want it used" case. Takes precedence over a DbUnitTester field and a DbUnitConfig.databaseTesterFactory().

Accepts a static field, the way a test case shared across every method in a class is typically declared. Declaring this alongside a DbUnitTester field, or more than one field with either marker at the same class level, is rejected as ambiguous.

For an instance reused across methods this way that also caches its own PrepAndExpectedTestCase.getReusableConnection() connection, the extension leaves that connection alone when a test aborts before configureTest() (e.g. a @DbUnitConfig value the instance's type cannot apply) - the instance keeps ownership rather than the extension closing a connection the next method would reuse. A single-use instance the extension constructs itself still has its connection closed on such a failure.

Marking this field does not, by itself, remove the need for a resolvable IDatabaseTester: the extension's own machinery (installing the @DbUnitProperty listener, and any IDatabaseTester parameter injection) needs one regardless of test case type. For an implementation whose type overrides PrepAndExpectedTestCase.getDatabaseTester()/ PrepAndExpectedTestCase.setDatabaseTester(org.dbunit.IDatabaseTester) - DefaultPrepAndExpectedTestCase does - one is found and wired on automatically when none is set yet. Any other implementation may override those same two methods to get the same automatic wiring; one that does not (their default is a no-op reporting no tester) needs DbUnitConfig.databaseTesterFactory() configured even when it manages its own connection internally - otherwise resolution fails with "No IDatabaseTester field found" for a test case that otherwise needs nothing external. A DbUnitTester field is not a substitute here: it is rejected outright alongside this annotation (see above), and even if it were not, DbUnitExtension's tester resolution does not consult one for this fallback. In that case the factory-resolved tester must be the exact instance this test case actually uses internally, or @DbUnitSetup/@DbUnitTearDown operations set on it may silently never reach what this test case actually runs.

Since:
3.6.0
Author:
Jeff Jensen
See Also: