Annotation Type DbUnitDataSet
This annotation can be applied at different levels to define datasets globally or locally:
- Class – applies to all test methods in the class.
- Method – applies only to the annotated test method.
Multiple datasets can be specified, and they will be loaded in the order
declared. Each entry can be a classpath resource or any location supported
by the underlying DbUnit IDataSet loader.
Example usage:
@DbUnitDataSet("/dataset/common.xml")
public class MyTest {
@Rule
public DbUnitRule dbUnitRule = new DbUnitRule(connectionFactory);
@Test
public void defaultDataSetIsLoaded() {
// Uses /dataset/common.xml
}
@Test
@DbUnitDataSet("/dataset/override/table1.xml")
public void methodSpecificDataSet() {
// Uses /dataset/override/table1.xml
}
}
Advanced: Instead of static files, you may provide one or more
DataSetProvider implementations through providers() to
build datasets programmatically. Each provider class must declare a
public no-argument constructor so the framework can instantiate it
reflectively.
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanIndicates whether datasets defined on parent scopes (package or class) should be merged with those declared on the current element.Class<? extends DataSetProvider>[]Optional programmatic dataset providers.booleanEnables discovery and loading ofDataSetProviderimplementations through the standard JavaServiceLoadermechanism.String[]Path(s) to dataset files to load.
-
Element Details
-
value
String[] valuePath(s) to dataset files to load. These are typically classpath resources such as"/dataset/users.xml".- Returns:
- one or more dataset file paths.
- Default:
{}
-
providers
Class<? extends DataSetProvider>[] providersOptional programmatic dataset providers. Each provider must implementDataSetProviderand have a public no-argument constructor.- Returns:
- an array of dataset provider classes.
- Default:
{}
-
useServiceLoader
boolean useServiceLoaderEnables discovery and loading ofDataSetProviderimplementations through the standard JavaServiceLoadermechanism.When set to
true(the default), the framework will scan the classpath for service declarations located atMETA-INF/services/com.github.mjeanroy.dbunit.core.dataset.DataSetProvider. Each discovered provider is instantiated (requiring a public no-argument constructor) and itsDataSetProvider.get()method is invoked to obtain anIDataSet. All datasets returned by these providers are then merged into the final dataset for the test.When set to
false, no service-loader lookup is performed and only the datasets specified byvalue()and/orproviders()are considered.Usage Note: This flag is optional. Disable it if you do not rely on service-loader–based dataset providers or if you want to avoid any runtime classpath scanning for performance.
- Returns:
trueto automatically discoverDataSetProviderservices;falseto skip service-loader discovery.
- Default:
true
-
inherit
boolean inheritIndicates whether datasets defined on parent scopes (package or class) should be merged with those declared on the current element.When
false(default), datasets defined here completely replace any datasets from superclasses or package-level annotations.When
true, datasets from parent annotations are inherited and merged with the ones defined here.- Returns:
trueto merge with parent datasets;falseto override.
- Default:
false
-