Annotation Type DbUnitDataSet


@Retention(RUNTIME) @Inherited @Documented @Target({METHOD,TYPE}) public @interface DbUnitDataSet
Data set annotation used to specify which DbUnit dataset(s) should be loaded before executing a test.

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 Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Indicates 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.
    boolean
    Enables discovery and loading of DataSetProvider implementations through the standard Java ServiceLoader mechanism.
    Path(s) to dataset files to load.
  • Element Details

    • value

      String[] value
      Path(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>[] providers
      Optional programmatic dataset providers. Each provider must implement DataSetProvider and have a public no-argument constructor.
      Returns:
      an array of dataset provider classes.
      Default:
      {}
    • useServiceLoader

      boolean useServiceLoader
      Enables discovery and loading of DataSetProvider implementations through the standard Java ServiceLoader mechanism.

      When set to true (the default), the framework will scan the classpath for service declarations located at META-INF/services/com.github.mjeanroy.dbunit.core.dataset.DataSetProvider. Each discovered provider is instantiated (requiring a public no-argument constructor) and its DataSetProvider.get() method is invoked to obtain an IDataSet. 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 by value() and/or providers() 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:
      true to automatically discover DataSetProvider services; false to skip service-loader discovery.
      Default:
      true
    • inherit

      boolean inherit
      Indicates 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:
      true to merge with parent datasets; false to override.
      Default:
      false