Skip to content

Getting Started

Marcel Schnelle edited this page Apr 7, 2019 · 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.

Using Kotlin for your build scripts? Check out this guide instead.

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.4.1"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.4.1"

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

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

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 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).all {
  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8
  }
}

Now you're ready to write JUnit 5!

Full example

Click to expand

Assumed File Structure

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

1) root/build.gradle

buildscript {
  repositories {
    google()
    // 1) Add JCenter repository
    jcenter()
  }
  dependencies {
    classpath "com.android.tools.build:gradle:3.4.0-rc03"
    // 2) Add Plugin dependency
    classpath "de.mannodermaus.gradle.plugins:android-junit5:1.4.1.0"
  }
}

allprojects {
  repositories {
    google()
    jcenter()
  }
}

2) module/build.gradle

apply plugin: "com.android.library"
// 3) Apply Plugin
apply plugin: "de.mannodermaus.android-junit5"

// 4a) Enable Java 8 (for Kotlin)
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8
  }
}

android {
  compileSdkVersion 28
  defaultConfig {
    minSdkVersion 15
    targetSdkVersion 28
    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.4.1"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.4.1"
}