Migrating from 1.0.x

Marcel Schnelle edited this page Jul 28, 2018 · 6 revisions

Some time has passed between the last proper release of this plugin, version 1.0.32, and the latest iteration, 1.2.0.0. In the meantime, a lot has charged under the hood. This guide gives some pointers on how to upgrade your integration of JUnit 5 to be compliant with the new version.

Update versions

First, the obvious one: Update your dependency to the latest version. Note the different version scheme, too!

buildscript {
  dependencies {
    classpath "de.mannodermaus.gradle.plugins:android-junit5:1.2.0.0"
  }
}

Going forward, releases will follow the JUnit Platform versioning, and include an independent patch version as the fourth component:

JUnit Platform version android-junit5 version
1.2.0 1.2.0.x

Remove bundled dependencies

Starting with Android Studio 3.1, the JUnit Platform bundled into the IDE has finally reached a stable state. Therefore, there is no need to manually patch outdated artifacts with newer stuff. When upgrading to android-junit5 1.2.0.0, you will need to include the dependencies on JUnit Jupiter and Vintage yourself. Furthermore, the deprecated embedded-runtime artifact will no longer be distributed.

1.0.32:

dependencies {
    testImplementation junit5.unitTests()
    testImplementation junit5.parameterized()

    testImplementation junit5.unitTestsRuntime()
}

1.2.0.0:

dependencies {
    // Before: junit5.unitTests()
    testImplementation "org.junit.jupiter:junit-jupiter-api:5.2.0"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.2.0"

    // Before: junit5.unitTests()
    testImplementation "junit:junit:4.12"
    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:5.2.0"

    // Before: junit5.parameterized()
    testImplementation "org.junit.jupiter:junit-jupiter-params:5.2.0"

    // There is no migration from junit5.unitTestsRuntime(), as it became obsolete in Android Studio 3.1.
    // Simply remove this line and you're good to go!
}

Review tasks

Since the release of Gradle 4.6, it's been possible to use the native Test tasks in conjunction with the JUnit Platform. Because of that, the old workaround of running JUnit 5 tests (using JavaExec tasks and the ConsoleLauncher provided by the JUnit Platform) is no longer necessary. As a result, the new version of the android-junit5 plugin no longer declares junitPlatformTestXXX tasks (with XXX representing a build variant). In case your build scripts configure these tasks manually by looking up these names, please move to the default task names provided by the Android Gradle Plugin instead.

Before:

junitPlatformTestDebug
junitPlatformTestRelease
// ...

After:

testDebugUnitTest
testReleaseUnitTest
// ...

Review configuration

Most of the DSL configuration provided by the plugin was changed or removed with the migration to native Gradle APIs for unit test tasks driven by JUnit 5. The APIs for test filtering now match the defaults provided by the build system, with only a few exceptions. Check the following section for infos on what to do with each of the old parameters.

Removed:

android.testOptions {
  junitPlatform {
    // Removed; specify explicitly through a dependency
    platformVersion "..."

    // Removed; specify explicitly through a dependency
    jupiterVersion "..."
    
    // Removed; specify explicitly through a dependency
    vintageVersion "..."
    
    // Removed; reports are located at the default location provided by the Android Gradle Plugin
    reportsDir "..."
    
    // Removed; shouldn't have had a big impact on Android users anyway
    logManager "..."
    
    // Removed; the tasks now run inside the standard test task
    enableStandardTestTask false
    
    // Removed; the tests now hook into the build system natively
    details "..."
    
    // Removed entirely; specify filters instead
    selectors {
      uri "..."
      uris "..."
      file "..."
      files "..."
      directory "..."
      directories "..."
      aPackage "..."
      packages "..."
      aClass "..."
      classes "..."
      method "..."
      methods "..."
      resource "..."
      resources "..."
    }
    
    // Removed entirely; specify on android.testOptions instead
    unitTests {
      returnDefaultValues = false
      includeAndroidResources = false
      all { test ->
        jvmArgs "--my-argument"
        systemProperty "my.system.property", "true"
        environment "MY_ENVIRONMENT", "development"
      }
    }
  }
}

Changed:

android.testOptions {
  junitPlatform {
    filters {
      // Changed; now called "includePattern" / "includePatterns"
      includeClassNamePatterns "..."
      
      // Changed; now called "excludePattern" / "excludePatterns"
      excludeClassNamePatterns "..."

      // Removed; specify with "includePattern" / "excludePattern" instead
      packages {
        include "..."
    	exclude "..."
      }

      // Changed; now called "includeTags" / "excludeTags" inside the filters block
      tags {
        include "..."
        exclude "..."
      }

      // Changed; now called "includeEngines" / "excludeEngines" inside the filters block
      engines {
        include "..."
        exclude "..."
      }
    }
  }
}

Unchanged:

android.testOptions {
  junitPlatform {
    // OK; still works the same way
    configurationParameter "key", "value"
    
    // Unchanged, but deprecated; these values don't have any effect anymore
    instrumentationTests {
      enabled true
      version "0.2.2"
    }
    
    // OK; still works the same way
    jacocoOptions {
      excludedClasses += []
      taskGenerationEnabled = true
      onlyGenerateTasksForVariants "debug", "release"
      xml {
        enabled true
        destination project.file()
      }
      html {
        enabled true
        destination project.file()
      }
      csv {
        enabled true
        destination project.file()
      }
    }
  }
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.