Search code examples
javaunit-testingjunitdbunit

Is there any way in Database Rider to reduce duplication of @DataSet configuration properties?


Is there any way in Database Rider to reduce duplication of @DataSet configuration properties?

For example, suppose we have:

@DataSet(value = "yml/test1.yml", cleanAfter = true, skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
public void test1() {
  //...
}

@DataSet(value = "yml/test2.yml", cleanAfter = true, skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
public void test2() {
  //...
}

Here as you can see cleanAfter and skipCleaningFor are duplicated. And this is just an example. The list of skipCleaningFor can be even longer. Having thousands of tests with the same list of skipped tables is a nightmare. Imagine we need to change this list, in thousands of places.

Extracting this list to some static final variable is not an option, because parameters to annotation can be only true inline constants (you will get compilation error).

Meta datasets are one of the ways of reducing duplication but they include ALL properties, and it's not possible to override some of the properties (like value in my example)

Merge datasets looks promising, but not sure...


Solution

  • Seems I figured out how to do it using merging datasets. First, we need some BaseTest for all our tests with the config of skipCleaningFor :

    @DataSet(value = "yml/empty.yml", skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
    class BaseTest
    

    Where empty.yml it's just some empty dataset to avoid error during tests execution. Then, in the test class, we put

    @DbUnit(mergeDataSets = true)
    @DBRider
    class MyTest extends BaseTest {
       @DataSet(value = "yml/test1.yml", cleanAfter = true)
       public void test1() {
         //...
       }
    
       @DataSet(value = "yml/test2.yml", cleanAfter = true)
       public void test2() {
         //...
       }
    

    And that's it - the config is no longer duplicated!