implementation 'org.junit.jupiter:junit-jupiter-api'
implementation 'org.junit.jupiter:junit-jupiter-engine'
implementation 'org.junit.platform:junit-platform-launcher'
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.
Also, we need to exclude the junit 4 dependencies which are shipped by default for Spring Boot 2.1 based projects.
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)
.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
-
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.
-
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.
Reference
Top articles in this category:
- Junit interview questions for SDET automation engineer
- Migrating Spring Boot tests from Junit 4 to Junit 5
- JUnit 5 Parameterized Tests
- Writing a simple Junit 5 test
- Rest Assured API Testing Interview Questions
- 50 Java Interview Questions for SDET Automation Engineer
- h2load for REST API benchmarking