Annotation Interface DbUnitPrep


@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented @Inherited public @interface DbUnitPrep
Specifies the dataset file(s) to load and insert into the database before each test method, seeding the state the test needs to run.

A method-level annotation overrides a class-level annotation on a per-test basis; the class annotation is inherited by subclasses. Pair with DbUnitSetup only when a setup operation other than the default DbUnitOperation.CLEAN_INSERT is needed - see DbUnitSetup for why the two are separate annotations.

Each path is resolved to an absolute classpath resource: a path already starting with / is used as-is; otherwise it is resolved relative to the test class's package, or to DbUnitConfig#dataSetBaseDir() when that is set. The file format is inferred from the extension by the configured DataFileLoader - see DbUnitConfig.dataFileLoader().

Example:


 @DbUnitTest
 class UserRepositoryTest {
     IDatabaseTester databaseTester;

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

     @Test
     @DbUnitPrep("/datasets/users.xml")
     void testFindAll() { ... }
 }
 
Since:
3.6.0
Author:
Jeff Jensen
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    A DataSetPathsProvider implementation to reflectively instantiate and ask for the prep dataset paths, for a path list shared across several test classes.
    Classpath resource paths for the prep dataset files to load.
  • Element Details

    • value

      String[] value
      Classpath resource paths for the prep 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 prep 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; drop @DbUnitPrep for a test that needs no dataset.

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