Skip to content

Getting Started

Marcel Schnelle edited this page Jul 31, 2021 · 11 revisions

This is a small guide designed to get you started with JUnit 5 integration in your Android project. For more detailed instructions and information on selected topics, please check the sidebar.

Step 0: Check requirements

Using this plugin comes with a few requirements. Before you start, make sure to update your environment:

  • Use Android Gradle Plugin 3.5.0 or higher
  • Use Gradle 6.1.1 or higher
  • Use Java 8

Step 1: Download plugin

In your project's build.gradle.kts file, add the plugin.

buildscript {
dependencies {
classpath("de.mannodermaus.gradle.plugins:android-junit5:<latest-version>")
}
}

Please replace <latest-version> with whatever comes up on top on the Releases page.

Step 2: Apply plugin

In your module's build.gradle.kts file, apply the plugin at the top, alongside your Android plugin.

plugins {
id("com.android.application") // Or library
id("de.mannodermaus.android-junit5")
}

Step 3: Add dependencies

Finally, add the JUnit 5 dependencies that you need to your module's build.gradle.kts, too.

dependencies {
// (Required) Writing and executing Unit Tests on the JUnit Platform
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
// (Optional) If you need "Parameterized Tests"
testImplementation("org.junit.jupiter:junit-jupiter-params:5.7.2")
// (Optional) If you also have JUnit 4-based tests
testImplementation("junit:junit:4.13.2")
testImplementation("org.junit.vintage:junit-vintage-engine:5.7.2")
}

Step 4: Configure Java 8

To leverage all the fancy features provided by JUnit 5, it might be necessary to manually configure your project for Java 8. To achieve this, you add the following to your build file:

android {
compileOptions {
sourceCompatibility =JavaVersion.VERSION_1_8
targetCompatibility =JavaVersion.VERSION_1_8
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget =JavaVersion.VERSION_1_8.toString()
}
}

Now you're ready to write JUnit 5!

Full example

Click to expand

Assumed File Structure

root/
|__ build/
|__ module/
|__ build/
|__ src/
|__ build.gradle.kts
|__ build.gradle.kts
|__ settings.gradle.kts

1) root/build.gradle.kts

buildscript {
repositories {
google()
// 1) Add Maven Central repository
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:4.2.2")
// 2) Add Plugin dependency
classpath("de.mannodermaus.gradle.plugins:android-junit5:1.7.1.1")
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}

2) module/build.gradle.kts

importorg.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.android.library")
id("kotlin-android")
// 3) Apply Plugin
id("de.mannodermaus.android-junit5")
}
// 4a) Enable Java 8 (for Kotlin)
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget =JavaVersion.VERSION_1_8.toString()
}
}
android {
compileSdk =30
defaultConfig {
minSdk =23
targetSdk =30
versionCode =1
versionName ="1.0"
testInstrumentationRunner ="androidx.test.runner.AndroidJUnitRunner"
}
// 4b) Enable Java 8 (for Java)
compileOptions {
sourceCompatibility =JavaVersion.VERSION_1_8
targetCompatibility =JavaVersion.VERSION_1_8
}
testOptions {
}
}
// 5) Configure JUnit 5, if necessary
junitPlatform {
// Configuration goes here!
}
dependencies {
// 6) Add JUnit 5 dependencies
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
}