Parameterized JUnit5 Test with all Classes with the same Baseclass

43 views Asked by At

I try to run some tests on all classes that extends the same baseclass like:

   classe Baseclass {
abstract int method();
}

class A extends Baseclass { int method(){...} }

class B extends Baseclass {int method() {..}}

class Testclass {
    @ParameterizedTest 
    @ValueSource( classes={T extends Baseclass})
    void test(Baseclass T){
      assertStuff(T..)}}

The Testclass should execute the test method with class A and B. When I add a Class C that extends BaseClass class C should automatically be tested to. Is there a way to do this ?

1

There are 1 answers

0
Ken Chan On BEST ANSWER

Think that @Tag can help you . Just tag the Baseclass with a tag such as :

@Tag("baseClassTest")
classe Baseclass {
  abstract int method();
}

Then depending on how you run the test , you can specify you only want to run the tests with the tag baseClassTest meaning that to run all tests the extends Baseclass. So if a new test class that extends BaseClass is added , it also will be covered.

  • For IntelliJ IDEA , you can refer to this.
  • For Maven , you can refer to this.
  • For Gradle , you can refer to this