At the time of this writing, there is no official support for JUnit 5 in Android projects. To solve this problem, an open source Gradle plugin named android-junit5 has been created (https://github.com/aurae/android-junit5). To use this plugin, first we need to specify the proper dependency in our build.gradle file:
buildscript {
dependencies {
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0"
}
}
In order to use this plugin in our project, we need to extend our project capabilities using the clause apply plugin in our build.gradle file:
apply plugin: "com.android.application"
apply plugin: "de.mannodermaus.android-junit5"
dependencies {
testCompile junitJupiter()
}
The android-junit5 plugin configures the junitPlatform task, attaching automatically attaches both the Jupiter and Vintage engines during the test execution phase. As an example, consider the following project example, as usual hosted on GitHub (https://github.com/bonigarcia/mastering-junit5/tree/master/junit5-android). The following is a screenshot of this project imported in Android Studio:
Now, we are going to create an Android JUnit run configuration of Android Studio. As can be seen in the screenshot, we use the option All in package referred to the package containing the tests (io.github.bonigarcia.myapplication in this example):
If we launch the aforementioned run configuration, all the tests of the project will be executed. These tests can use the JUnit 4 programming model (Vintage) and even the JUnit 5 (Jupiter) in a seamless way: