A Gradle plugin that allows for the execution of JUnit 5 tests in Android environments.
buildscript {
dependencies {
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.21"
}
}
Snapshots of the development version are available through Sonatype's snapshots
repository.
apply plugin: "com.android.application"
apply plugin: "de.mannodermaus.android-junit5"
dependencies {
testImplementation junit5()
// (Optional) If you need "parameterized tests"
testImplementation junit5Params()
// (Optional) For running tests inside Android Studio 3.x (see below for details)
testCompileOnly junit5EmbeddedRuntime()
}
This plugin configures a junitPlatformTest
task for each registered build variant of a project.
It automatically attaches both the Jupiter & Vintage Engines during the execution phase of your tests as well.
More instructions on how to write JUnit 5 tests can be found in their User Guide. Furthermore, this repository provides a small showcase of the functionality provided by JUnit 5 here.
Note:
The following section deals with fixing Test Execution within Android Studio 3. Running your JUnit 5 tests directly from Android Studio 2.3.3 and earlier will not work: You will encounter an
AbstractMethodError
when trying to do so (more information here).The cause of this error is similar in nature to the one described below, and related to outdated APIs. Unlike that issue though, we can't fix the
AbstractMethodError
inside IntelliJ's internal runtime in the same way. Therefore, please resort to using Gradle for unit testing in Android Studio 2.
All versions up to and including Android Studio 3.1 Canary are built on a version of IntelliJ IDEA that depends on outdated JUnit 5 APIs. Therefore, your tests will fail with an Exception similar to the following when you try to launch your tests from inside the IDE (using an Android JUnit Run Configuration):
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:42)
...
To work around this, there is a separate dependency you can add to the test scope of your project in Android Studio 3. It provides its own copy of the JUnit 5 Runtime provided by a more recent build of IntelliJ, overriding the one embedded in Android Studio.
To use this, add the following line alongside the other junit5()
dependencies:
dependencies {
testCompileOnly junit5EmbeddedRuntime()
}
Inside the configuration closure applied by the plugin, you can specify the same properties as you would for a Java-based project with the JUnit Platform Gradle plugin. However, there are some additional properties that you can apply:
junitPlatform {
// The JUnit Jupiter dependency version to use; matches the platform's version by default
jupiterVersion "5.0.2"
// The JUnit Vintage Engine dependency version to use; matches the platform's version by default
vintageVersion "4.12.2"
}
If the plugin detects the usage of JaCoCo inside a project that it's being applied to, it will automatically configure additional tasks to report the unit test coverage of your application based on its JUnit 5 tests. There is no additional setup required to enable this behaviour. You can however customize the reports JaCoCo should generate.
Configuration is applied through the jacoco
clause inside the plugin's DSL:
apply plugin: "jacoco"
junitPlatform {
jacoco {
csvReport true
xmlReport true
htmlReport true
}
}
android-junit5
is distributed with multiple Open Source licenses:
android-junit5-embedded-runtime
uses Apache License v2.0- All other modules use Eclipse Public License v2.0
Please see the LICENSE.md
files in the subfolders for more details.