Skip to content

Getting Started

Marcel Schnelle edited this page Jul 28, 2018 · 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.2.0-alpha18 or higher
  • Use Gradle 4.7 or higher
  • Use Java 8

Step 1: Download plugin

In your project's build.gradle 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 file, apply the plugin at the top, below your Android plugin.

apply plugin: "de.mannodermaus.android-junit5"

Step 3: Add dependencies

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

dependencies {
  // (Required) Writing and executing Unit Tests on the JUnit Platform
  testImplementation "org.junit.jupiter:junit-jupiter-api:5.2.0"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.2.0"

  // (Optional) If you need "Parameterized Tests"
  testImplementation "org.junit.jupiter:junit-jupiter-params:5.2.0"

  // (Optional) If you also have JUnit 4-based tests
  testImplementation "junit:junit:4.12"
  testImplementation "org.junit.vintage:junit-vintage-engine:5.2.0"
}

Step 4 (optional): 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 module's build.gradle:

// For Java
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

// For Kotlin
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).matching { "$it".contains("UnitTest") }.all {
  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8
  }
}

Now you're ready to write JUnit 5!