Junit 5 Platform Launcher API

Carvia Tech | September 08, 2019 | 2 min read | 0 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. SDET: JUnit interview questions for automation engineer
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. Using h2load for REST API benchmarking
  4. SDET Java Role - Essential Skills
  5. Check if the given number is Armstrong Number in Java
  6. Write a program to reverse a string using recursion in Java
  7. How to reverse a number in Java



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