Annotation Interface DbUnitExpected


@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented @Inherited public @interface DbUnitExpected
Specifies the expected dataset file(s) to verify actual database state against after each test method, and (optionally) which tables and rules to verify with. Presence of this annotation is what switches the test from the setup/teardown path onto the PrepAndExpectedTestCase prep/expected path - which runs dbUnit's expected-dataset comparison for you. Leave it off, and the test method does its own asserting.

A method-level annotation overrides a class-level annotation on a per-test basis; the class annotation is inherited by subclasses. Valid with no DbUnitPrep at all - the database state already present is what gets verified.

Which tables to verify, and with what rules, resolves through the following forms so the common cases stay short - highest-priority form first:

  1. verify() together with verifyDefinitions() or verifyTables() - rejected as ambiguous.
  2. verifyDefinitions(), else - when this method declares neither it nor verify() - DbUnitConfig.verifyDefinitions() as the class-level default - every definition in whichever named catalog class(es) is in play, narrowed to verifyTables() when that is also given.
  3. verify() - full per-table rules, inline.
  4. verifyTables(), with no catalog in play (neither this method's own verifyDefinitions() nor a class-level DbUnitConfig.verifyDefinitions()) - a default VerifyTableDefinition per named table.
  5. None of the above - a default VerifyTableDefinition per table in the expected dataset.

Example:


 @DbUnitTest
 class AccountRepositoryTest {
     IDatabaseTester databaseTester;

     AccountRepositoryTest() throws ClassNotFoundException {
         databaseTester = new JdbcDatabaseTester("driver", "url", "user", "pass");
     }

     @Test
     @DbUnitPrep("/dbunit/accounts/prep.xml")
     @DbUnitExpected(value = "/dbunit/accounts/expected.xml",
             verifyTables = {"ACCOUNT", "TRANSACTION"})
     void testWithdraw_sufficientBalance_decrementsBalance() { ... }
 }
 
Since:
3.6.0
Author:
Jeff Jensen
See Also:
  • Element Details

    • value

      String[] value
      Classpath resource paths for the expected dataset files to load.

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

      Returns:
      Zero or more dataset resource paths.
      Default:
      {}
    • provider

      Class<? extends DataSetPathsProvider> provider
      A DataSetPathsProvider implementation to reflectively instantiate and ask for the expected dataset paths, for a path list shared across several test classes.

      Mutually exclusive with value(); setting both is rejected. The provider returning null or an empty array is rejected too - it is wired to supply paths, so supplying none is a misconfiguration.

      Returns:
      The provider class; the interface itself (the default) means "not set".
      Default:
      org.dbunit.util.fileloader.DataSetPathsProvider.class
    • verifyTables

      String[] verifyTables
      Names of the tables to verify, using default verification rules - or, when verifyDefinitions() is also set, the subset of that catalog's tables to select.
      Returns:
      The table names; empty (the default) means "every table", the exact meaning depending on which other members are set - see the class Javadoc.
      Default:
      {}
    • verify

      Full per-table verification rules, inline.
      Returns:
      The table definitions; empty (the default) defers to verifyTables() or verifyDefinitions().
      Default:
      {}
    • verifyDefinitions

      Class<?>[] verifyDefinitions
      One or more catalog classes to read shared VerifyTableDefinition constants from - either a VerifyTableDefinitionsProvider implementation, or a plain class exposing public static final VerifyTableDefinition fields. Overrides DbUnitConfig.verifyDefinitions() when both are set.
      Returns:
      The catalog classes; empty (the default) means "not set".
      Default:
      {}