Annotation Interface DbUnitSpringTest


@Target(TYPE) @Retention(RUNTIME) @Documented @Inherited @TestExecutionListeners(listeners=DbUnitTestExecutionListener.class, mergeMode=MERGE_WITH_DEFAULTS) public @interface DbUnitSpringTest
One-line opt-in for DbUnitTestExecutionListener: @DbUnitSpringTest is exactly @TestExecutionListeners(listeners = DbUnitTestExecutionListener.class, mergeMode = MergeMode.MERGE_WITH_DEFAULTS).

The mergeMode is why this annotation exists rather than registering DbUnitTestExecutionListener with a bare @TestExecutionListeners: that annotation's own default, TestExecutionListeners.MergeMode.REPLACE_DEFAULTS, replaces Spring's default listeners entirely, silently dropping dependency injection and transaction management for every test class that uses it without also re-listing every default listener by hand.

Spring honors only one @TestExecutionListeners declaration per class. If the class declares its own, this annotation has no effect. If it carries another annotation composed with one, Spring honors only the first it finds, going by declaration order, and silently drops the other's listeners. Either way the DbUnit listener can end up never registered, every @DbUnit* annotation is ignored, and the tests run with no prep, verification or teardown. Add DbUnitTestExecutionListener to that one declaration instead, with mergeMode = MERGE_WITH_DEFAULTS. A subclass's own declaration is not affected, as its listeners are merged with those it inherits.

Also the natural carrier for a project's own composed annotation, bundling @DbUnitSpringTest with shared @DbUnitConfig and lifecycle annotations, the same way org.dbunit.junit.jupiter.DbUnitTest is:


 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.TYPE)
 @Inherited
 @DbUnitSpringTest
 @DbUnitConfig(dataFileLoader = FlatXmlDataFileLoader.class)
 @DbUnitSetup(operation = DbUnitOperation.DELETE_ALL)
 public @interface AppSpringDatabaseTest {}
 
Since:
3.6.0
Author:
Jeff Jensen
See Also: