Learn to configure junit 5 with gradle, its different modules and how to use them to create and execute tests.
1. JUnit 5 Gradle Dependency
To run JUnit 5 tests through gradle, you will need minimum two dependencies.
JUnit Jupiter Engine Dependency
JUnit jupiter is required to have two dependencies i.e.
junit-jupiter-api
andjunit-jupiter-engine
.junit-jupiter-api
has junit annotations (e.g. @Test) to write tests and extensions andjunit-jupiter-engine
has test engine implementation which is required at runtime to execute the tests.Internally,
junit-jupiter-engine
is dependent onjunit-jupiter-api
, so addingjunit-jupiter-engine
only brings both dependencies into classpath.We can learn about internal dependencies between various junit jars in this image.
dependencies { testRuntime("org.junit.jupiter:junit-jupiter-engine:5.5.2") testRuntime("org.junit.platform:junit-platform-runner:1.5.2") } test { useJUnitPlatform() }
JUnit Platform Runner Dependency
We will need
junit-platform-runner
for executing tests and test suites on the JUnit Platform in a JUnit 4 environment.Internally,
junit-platform-runner
is dependent onjunit-platform-suite-api
andjunit-platform-launcher
, so addingjunit-jupiter-engine
only brings all three dependencies into classpath.testRuntime("org.junit.platform:junit-platform-runner:1.5.2")
2. Execute JUnit 4 Tests with JUnit 5
To execute junit 4 tests in junit 5 environment, you will need to include junit-vintage-engine
dependency. JUnit Vintage provides a TestEngine
for running JUnit 3 and JUnit 4 based tests on the platform.
dependencies { //To run JUnit 3 and junit 4 tests testCompile("junit:junit:4.12") testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4") }
By configuring above in build.gradle
, now you can run your old junit 3 or junit 4 tests with junit 5.
3. JUnit 5 Gradle Example
A sample build.gradle
file for running tests built with junit 5 is as follow:
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2' } } repositories { mavenCentral() } ext.junit4Version = '4.12' ext.junitVintageVersion = '4.12.2' ext.junitPlatformVersion = '1.0.2' ext.junitJupiterVersion = '5.0.2' ext.log4jVersion = '2.9.0' apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.junit.platform.gradle.plugin' jar { baseName = 'junit5-gradle-consumer' version = '1.0.0-SNAPSHOT' } compileTestJava { sourceCompatibility = 1.8 targetCompatibility = 1.8 options.compilerArgs += '-parameters' } junitPlatform { // platformVersion '1.0.2' filters { engines { // include 'junit-jupiter', 'junit-vintage' // exclude 'custom-engine' } tags { // include 'fast' exclude 'slow' } // includeClassNamePattern '.*Test' } // configurationParameter 'junit.jupiter.conditions.deactivate', '*' // enableStandardTestTask true // reportsDir file('build/test-results/junit-platform') // this is the default logManager 'org.apache.logging.log4j.jul.LogManager' } dependencies { // JUnit Jupiter API and TestEngine implementation testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") // If you also want to support JUnit 3 and JUnit 4 tests testCompile("junit:junit:${junit4Version}") testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}") // To avoid compiler warnings about @API annotations in JUnit code testCompileOnly('org.apiguardian:apiguardian-api:1.0.0') // To use Log4J's LogManager testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}") testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}") // Only needed to run tests in an (IntelliJ) IDE(A) that bundles an older version testRuntime("org.junit.platform:junit-platform-launcher:${junitPlatformVersion}") } task wrapper(type: Wrapper) { description = 'Generates gradlew[.bat] scripts' gradleVersion = '4.3.1' }
Ref : Git
Drop me your questions in comments section.
Happy Learning !!
Leave a Reply