Annotation Interface DbUnitSetup


@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented @Inherited public @interface DbUnitSetup
Specifies the database operation to apply before each test method, alongside DbUnitPrep for the dataset to apply it to.

A method-level annotation overrides a class-level annotation on a per-test basis; the class annotation is inherited by subclasses. Declaring only DbUnitPrep is the common case: operation() defaults to DbUnitOperation.CLEAN_INSERT already, so most tests never need this annotation at all.

Data (DbUnitPrep) and operation (DbUnitSetup) are deliberately separate annotations rather than one fused annotation, so a class-level operation survives a method-level DbUnitPrep instead of silently reverting to the default the moment a method declares its own files.

On the prep/expected path (DbUnitExpected declared), this operation is applied to the resolved IDatabaseTester, which only reaches the actual setup call when the test case reads its own setup operation from that same tester the way DefaultPrepAndExpectedTestCase does. A DbUnitTestCase field injecting a different implementation that manages its own tester internally leaves this annotation silently unapplied there.

Example — a class-wide DbUnitOperation.REFRESH operation, with each test method naming its own prep file:


 @DbUnitTest
 @DbUnitSetup(operation = DbUnitOperation.REFRESH)
 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
    The database operation to apply during setup.