Annotation Interface DbUnitConfig


@Target(TYPE) @Retention(RUNTIME) @Documented @Inherited public @interface DbUnitConfig
Class-level wiring for annotation-driven dbUnit test configuration: which DataFileLoader loads dataset files, how to obtain a tester via DatabaseTesterFactory when no field supplies one, which PrepAndExpectedTestCase implementation drives the prep/expected path, DatabaseConfig properties, and the shared table-definition catalog.

For configuration shared across many test classes, compose one custom annotation carrying @DbUnitTest, this annotation, and the lifecycle annotations, rather than repeating them on every class:


 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.TYPE)
 @Inherited
 @DbUnitTest
 @DbUnitConfig(dataFileLoader = FlatXmlDataFileLoader.class)
 @DbUnitSetup(operation = DbUnitOperation.REFRESH)
 public @interface AppDatabaseTest {}
 
Since:
3.6.0
Author:
Jeff Jensen
See Also:
  • Element Details

    • dataFileLoader

      Class<? extends DataFileLoader> dataFileLoader
      The DataFileLoader implementation used to load every DbUnitPrep and DbUnitExpected dataset file, reflectively instantiated with its no-arg constructor.

      On the prep/expected path (DbUnitExpected declared), a freshly-constructed prepAndExpectedTestCase() always receives this value through its constructor, regardless of implementation. A DbUnitTestCase field injecting an already-built instance also receives it, but only when its type overrides PrepAndExpectedTestCase.setDataFileLoader(DataFileLoader) - DefaultPrepAndExpectedTestCase does; naming a non-default loader here for a different injected implementation fails fast with IllegalStateException instead of silently loading with whatever loader that instance was already constructed with.

      Returns:
      The loader class; defaults to FileExtensionDataFileLoader.
      Default:
      org.dbunit.util.fileloader.FileExtensionDataFileLoader.class
    • databaseTesterFactory

      Class<? extends DatabaseTesterFactory> databaseTesterFactory
      A DatabaseTesterFactory implementation to reflectively instantiate (with its no-arg constructor) and ask to create the IDatabaseTester to use, when neither a DbUnitTestCase nor a DbUnitTester field is declared.
      Returns:
      The factory class; the interface itself (the default) means "not set", falling through to the plain field auto-scan.
      Default:
      org.dbunit.DatabaseTesterFactory.class
    • prepAndExpectedTestCase

      Class<? extends PrepAndExpectedTestCase> prepAndExpectedTestCase
      The PrepAndExpectedTestCase implementation to reflectively instantiate for the prep/expected path when no DbUnitTestCase field supplies one already, via a constructor accepting (DataFileLoader, IDatabaseTester, boolean) - the same shape DefaultPrepAndExpectedTestCase itself has.

      In practice a DefaultPrepAndExpectedTestCase subclass, overriding a hook such as setUpDatabaseConfig(). A from-scratch PrepAndExpectedTestCase would have to reimplement dataset loading, comparison, and cleanup, and override the six @DbUnitConfig-driven setters (setDataFileLoader, setFailureHandler, setDatabaseConfigProperties, setCloseConnectionAfterTest, setRowCountCheckOverride, clearRowCountCheckOverride) - one that does not fails fast at test time, naming the missing setter.

      Returns:
      The test case class; defaults to DefaultPrepAndExpectedTestCase.
      Default:
      org.dbunit.DefaultPrepAndExpectedTestCase.class
    • properties

      DbUnitProperty[] properties
      Inline DatabaseConfig property name/value pairs to apply.

      Mutually exclusive with propertiesProvider(); setting both is rejected.

      On the prep/expected path (DbUnitExpected declared), only takes effect on a prepAndExpectedTestCase() whose type overrides PrepAndExpectedTestCase.setDatabaseConfigProperties(java.util.Properties) - DefaultPrepAndExpectedTestCase does. A DbUnitTestCase field injecting a different implementation fails fast with IllegalStateException instead, since that path has no other route to the connection at all. On the setup/teardown path (no DbUnitExpected), this restriction does not apply - every value always reaches the tester's connection.

      On the prep/expected path, a DefaultPrepAndExpectedTestCase subclass that overrides setUpDatabaseConfig(DatabaseConfig) must call super.setUpDatabaseConfig(config) - the pre-3.6.0 way to configure a DatabaseConfig - or these values are silently dropped: they are applied through that same setUpDatabaseConfig() hook. The extension logs a warning when it sees such an override together with properties()/propertiesProvider(), since it cannot tell from reflection alone whether the super call is present.

      Returns:
      The properties; empty (the default) applies none.
      Default:
      {}
    • propertiesProvider

      Class<? extends DatabaseConfigPropertiesProvider> propertiesProvider
      A DatabaseConfigPropertiesProvider implementation to reflectively instantiate (with its no-arg constructor) and ask for the DatabaseConfig properties to apply, for properties shared across several test classes.

      Mutually exclusive with properties(); setting both is rejected.

      Subject to the same restriction on the prep/expected path that properties() documents.

      Returns:
      The provider class; the interface itself (the default) means "not set".
      Default:
      org.dbunit.database.DatabaseConfigPropertiesProvider.class
    • verifyDefinitions

      Class<?>[] verifyDefinitions
      One or more catalog classes to read shared VerifyTableDefinition constants from, as the class-level default for every method's DbUnitExpected.verifyDefinitions(). See DbUnitExpected for how the two resolve independently and what "catalog" means.
      Returns:
      The catalog classes; empty (the default) means "not set".
      Default:
      {}
    • dataSetBaseDir

      String dataSetBaseDir
      A classpath directory prefix applied to every DbUnitPrep and DbUnitExpected path that neither starts with / nor is otherwise absolute, ahead of the test-class-package default.
      Returns:
      The base directory; empty (the default) means "not set".
      Default:
      ""
    • failureHandler

      Class<? extends FailureHandler> failureHandler
      The FailureHandler to hand verification failures to, in place of dbUnit's own default.

      Only takes effect on a prepAndExpectedTestCase() whose type overrides PrepAndExpectedTestCase.setFailureHandler(FailureHandler) - DefaultPrepAndExpectedTestCase does. A DbUnitTestCase field injecting a different implementation fails fast with IllegalStateException instead, naming the implementation and this attribute, since a silent no-op here would otherwise leave the configured handler applied nowhere.

      Returns:
      The failure handler class, reflectively instantiated with its no-arg constructor; the interface itself (the default) means "not set".
      Default:
      org.dbunit.assertion.FailureHandler.class
    • closeConnectionAfterTest

      boolean closeConnectionAfterTest
      Whether the connection this executor resolves - for the prep/expected path, the row count check, or parameter injection - is closed after each test.

      Set to false when the IDatabaseTester shares a CachingConnectionProvider across test methods, so this test does not close a connection other tests still expect to reuse. The connection is also left open, regardless of this attribute, when the tester's IOperationListener is IOperationListener.NO_OP_OPERATION_LISTENER - the established, pre-existing signal that a tester's connection is managed elsewhere - so a connection already protected that way needs no explicit false here.

      On the prep/expected path, a freshly constructed prepAndExpectedTestCase() - no DbUnitTestCase field supplies one already - always receives this value through its (DataFileLoader, IDatabaseTester, boolean) constructor, regardless of implementation. A DbUnitTestCase field injecting an already-built instance receives it too, but only when its type overrides PrepAndExpectedTestCase.setCloseConnectionAfterTest(boolean) - DefaultPrepAndExpectedTestCase does; setting this to false for a different injected implementation logs a warning rather than failing fast, since - unlike the other @DbUnitTestCase-injected setters here - this executor's own connection (for the row count check or parameter injection) still honors the value regardless; only that test case's own connection handling might not.

      Returns:
      True to close the connection after each test; defaults to true.
      Default:
      true
    • injectConnectionParameter

      boolean injectConnectionParameter
      Whether the JUnit binding resolves a bare Connection parameter on this test. Default true.

      Set to false when another extension on the same test - Spring's SpringExtension, Testcontainers, a JPA or JDBC test harness - also resolves java.sql.Connection: JUnit Jupiter rejects two ParameterResolvers competing for one parameter with "discovered multiple competing ParameterResolvers", and dbUnit yields the common Connection type rather than force the other extension to. The dbUnit-specific IDatabaseConnection parameter is still resolved regardless - no other framework claims that type - so inject that and call its getConnection() for the JDBC connection.

      Returns:
      True to resolve a java.sql.Connection parameter; defaults to true.
      Default:
      true