Junit 5 Platform Launcher API

Carvia Tech | September 08, 2019 | 2 min read | 428 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.

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.

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)
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);

    public static void main(String[] args) {
        final Junit5Runner runner = new Junit5Runner();
        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


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. SDET: JUnit interview questions for automation engineer
  2. Rest Assured API Testing Interview Questions
  3. 50 SDET Java Interview Questions & Answers
  4. SDET Java Coding Challenges
  5. JUnit 5 Parameterized Tests
  6. Migrating Spring Boot tests from Junit 4 to Junit 5
  7. Writing a simple Junit 5 test

Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info