Package org.dbunit.annotation
Annotation 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:
verify()together withverifyDefinitions()orverifyTables()- rejected as ambiguous.verifyDefinitions(), else - when this method declares neither it norverify()-DbUnitConfig.verifyDefinitions()as the class-level default - every definition in whichever named catalog class(es) is in play, narrowed toverifyTables()when that is also given.verify()- full per-table rules, inline.verifyTables(), with no catalog in play (neither this method's ownverifyDefinitions()nor a class-levelDbUnitConfig.verifyDefinitions()) - a defaultVerifyTableDefinitionper named table.- None of the above - a default
VerifyTableDefinitionper 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:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionClass<? extends DataSetPathsProvider> ADataSetPathsProviderimplementation to reflectively instantiate and ask for the expected dataset paths, for a path list shared across several test classes.String[]Classpath resource paths for the expected dataset files to load.Full per-table verification rules, inline.Class<?>[]One or more catalog classes to read sharedVerifyTableDefinitionconstants from - either aVerifyTableDefinitionsProviderimplementation, or a plain class exposingpublic static final VerifyTableDefinitionfields.String[]Names of the tables to verify, using default verification rules - or, whenverifyDefinitions()is also set, the subset of that catalog's tables to select.
-
Element Details
-
value
String[] valueClasspath 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> providerADataSetPathsProviderimplementation 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 returningnullor 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[] verifyTablesNames of the tables to verify, using default verification rules - or, whenverifyDefinitions()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
DbUnitVerifyTable[] verifyFull per-table verification rules, inline.- Returns:
- The table definitions; empty (the default) defers to
verifyTables()orverifyDefinitions().
- Default:
{}
-
verifyDefinitions
Class<?>[] verifyDefinitionsOne or more catalog classes to read sharedVerifyTableDefinitionconstants from - either aVerifyTableDefinitionsProviderimplementation, or a plain class exposingpublic static final VerifyTableDefinitionfields. OverridesDbUnitConfig.verifyDefinitions()when both are set.- Returns:
- The catalog classes; empty (the default) means "not set".
- Default:
{}
-