Junit 5 execute test on attribute test class

Viewed 122

Hi i got an abstract BaseTest and a second with 2 attribute implementation of this BaseTest

I want to call all @Test on attribute class.

some Code :

public abstract class BaseTest<T> {

 public abstract T getInstance();
@BeforeEach
 public void setup(){
   this.instance = getInstance();
 }
@Test
all test methods ....

I want to create a second class with 2 implementation of this BaseTest like

class TwoImplOfBaseTest {
private BaseTest<Integer> testA;
private BaseTest<String> testB;

 @Test
 public void testAll(){
 //write something like testA.doAllTest();
     //write something like testB.doAllTest();

}

thanks for reading

2 Answers

I think you are over complicating it. If i understand correctly you have BaseTest class because of common test cases between A and B. @Test, and almost all other junit annotations, is inherited from super classes, unless the method is overridden. In this case all you need is creating concrete implementations of BaseTest. JUnit will initialize test class instances, run all @Test methods, etc.

public class TestA extends BaseTest<Integer> {
  //init stuff if needed
}

And other class

public class TestB extends BaseTest<String> {
  //init stuff if needed
}

And that's it, the junit runner will run all @Test methods from BaseTest for TestA and then for TestB.

If your objective is to run all @Test methods that are in an abstract class, and all extending classes, then that is already what happens. You can test this through any IDE (Intellij/Eclipse)

If however the objective is to programmatically select N classes, and run their methods from Java directly, then you can do so as described below:

In my opinion, it is best to follow the other answer, as this allows report plugins to visually show you what has passed/failed, and statistics.

It depends on what you are trying to achieve:


 public class RunJUnit5TestsFromJava {

      SummaryGeneratingListener listener = new SummaryGeneratingListener();
  
      public void runASingleClass() {

          LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
              .selectors(selectClass(FirstUnitTest.class))
              .build();
            Launcher launcher = LauncherFactory.create();
            TestPlan testPlan = launcher.discover(request);
            launcher.registerTestExecutionListeners(listener);
            launcher.execute(request);
        }

     public void runAllClassesUnderPackage() {

            LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
              .selectors(selectPackage("com.package.where.your.test.classes.are"))
              .filters(includeClassNamePatterns(".*Test"))
              .build();
            Launcher launcher = LauncherFactory.create();
            TestPlan testPlan = launcher.discover(request);
            launcher.registerTestExecutionListeners(listener);
            launcher.execute(request);
        }
    }

 
    RunJUnit5TestsFromJava runner = new RunJUnit5TestsFromJava();
    runner.runAll();
    
    TestExecutionSummary summary = runner.listener.getSummary();
    summary.printTo(new PrintWriter(System.out));
    

Reference: https://www.baeldung.com/junit-tests-run-programmatically-from-java

Related