Junit 5 Platform Launcher API

Upasana | September 08, 2019 | 2 min read | 516 views


Running Junit5 based tests programmatically from java/kotlin code based on selected package/class and tag filters using Junit Platform Launcher API.

Setting Gradle dependencies

We need to add the below dependencies to gradle based Spring Boot project for enabling Junit 5 support.

build.gradle
implementation 'org.junit.jupiter:junit-jupiter-api'
implementation 'org.junit.jupiter:junit-jupiter-engine'
implementation 'org.junit.platform:junit-platform-launcher'

Also, we need to exclude the junit 4 dependencies which are shipped by default for Spring Boot 2.1 based projects.

Exclude Junit4 from classpath
implementation("org.springframework.boot:spring-boot-starter-test") {
    exclude module: "junit"
}

Executing tests programmatically

We will use LauncherDiscoveryRequest to build a request that will be submitted to Junit TestEngine for test case discovery.

Junit5Runner.java
public class Junit5Runner {
    SummaryGeneratingListener listener = new SummaryGeneratingListener();

    public void runOne() {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(selectClass(SampleTest.class))   (1)
                .selectors(selectPackage("com.shunya"))     (2)
                .filters(TagFilter.includeTags("security")) (3)
                .build();
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);
    }

    public static void main(String[] args) {
        final Junit5Runner runner = new Junit5Runner();
        runner.runOne();
        TestExecutionSummary summary = runner.listener.getSummary();
        summary.printTo(new PrintWriter(System.out));
    }
}
1 Select class that we want to include for running testcases
2 Select package that we want to include for running testcases
3 Select Tags, only testcases tagged with given name will be filtered for execution

Output

When we run the above program, following output will be printed to the console. This output is produced by SummaryGeneratingListener

Test run finished after 2410 ms
[         2 containers found      ]
[         0 containers skipped    ]
[         2 containers started    ]
[         0 containers aborted    ]
[         2 containers successful ]
[         0 containers failed     ]
[         6 tests found           ]
[         0 tests skipped         ]
[         6 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         6 tests failed          ]

Important points

  1. Junit jupyter TestEngine will create a new instance of test class before invoking each test method. This helps making tests independent, as you can’t rely on execution order.

  2. Kotlin might be a better choice for writing testcases as tests written in Kotlin are more concise, readable and uses modern language features. So you might think of migrating to Kotlin.


Top articles in this category:
  1. Junit interview questions for SDET automation engineer
  2. Migrating Spring Boot tests from Junit 4 to Junit 5
  3. JUnit 5 Parameterized Tests
  4. Writing a simple Junit 5 test
  5. Rest Assured API Testing Interview Questions
  6. 50 Java Interview Questions for SDET Automation Engineer
  7. h2load for REST API benchmarking

Recommended books for interview preparation:

Find more on this topic: