Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 5 for Android testing

How to configure JUnit 5 for Android unit testing? I tried:

testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0")

But it doesn't work, when I run previous the simplest unit test:

@Test
public void addition_isCorrect() throws Exception {
    assertEquals(4, 2 + 2);
}

I get error:

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
    at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:31)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:36)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1
Empty test suite.
like image 261
mac229 Avatar asked Sep 11 '17 17:09

mac229


People also ask

Does JUnit 5 support Android?

A Gradle plugin that allows for the execution of JUnit 5 tests in Android environments using Android Gradle Plugin 4.0. 0 or later.

How do you write JUnit test cases for Android?

You write your local unit test class as a JUnit 4 test class. To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.

Is JUnit 4 better than JUnit 5?

Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.

Does JUnit 5 support java8?

JUnit 5 tests will run with Java 8 or above.


3 Answers

Refer to this issue and this issue regarding JUnit 5 support on Android.

Add this build script dependency in your top-level build.gradle[.kts] file:

buildscript {
  dependencies {
    classpath("de.mannodermaus.gradle.plugins:android-junit5:1.8.2.1")
  }
}

And add these lines in your app or library build.gradle[.kts] file:

plugins {
  // ...
  id("de.mannodermaus.android-junit5")
}

dependencies {
  // Aggregator dependency on JUnit api, engine, and params
  testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
  // (Optional) If you also have JUnit 4-based tests
  testImplementation("junit:junit:4.13.2")
  testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.9.1")
}

You can use the above solution if you are using Android Gradle plugin 3.2.0 or higher and Gradle 4.7 or higher. For more information refer to the plugin GitHub page.

Extended answer

If you want to use JUnit 5 in your instrumented tests (androidTest source set) do the following:

  1. Add these dependencies to your app or libray build script:
androidTestImplementation("de.mannodermaus.junit5:android-test-core:1.3.0")
androidTestRuntimeOnly("de.mannodermaus.junit5:android-test-runner:1.3.0")
  1. Use ActivityScenarioExtension instead of ActivityScenarioRule in your test classes:
import de.mannodermaus.junit5.ActivityScenarioExtension

class MyActivityTest {

  @JvmField
  @RegisterExtension
  val scenarioExtension = ActivityScenarioExtension.launch<MyActivity>()

  @Test
  fun test1() {
    val scenario = scenarioExtension.scenario
    scenario.moveToState(Lifecycle.State.RESUMED)
    assertThat(scenario.state).isEqualTo(Lifecycle.State.RESUMED)
  }

  @Test
  fun test2(scenario: ActivityScenario<MyActivity>) {
    scenario.moveToState(Lifecycle.State.RESUMED)
    assertThat(scenario.state).isEqualTo(Lifecycle.State.RESUMED)
  }
}
like image 156
Mahozad Avatar answered Oct 21 '22 20:10

Mahozad


Android Studio is based on IDEA, correct?

Then see this answer here https://stackoverflow.com/a/46161799/1431016 or read directly the user-guide at http://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-idea

Summary:

// Only needed to run tests in an IntelliJ IDEA that bundles an older version
testImplementation("org.junit.platform:junit-platform-launcher:1.0.0")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.0.0")
testImplementation("org.junit.vintage:junit-vintage-engine:4.12.0")

I didn't try this project yet, but it could help you using JUnit 5 on Android: https://github.com/aurae/android-junit5

like image 5
Sormuras Avatar answered Oct 21 '22 21:10

Sormuras


USE ALWAYS THE OFFICIAL VERSION

First go to the official documentation of Junit here : https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api you will see all latest version like this enter image description here

then in your app/gradle add this dependencie :

testImplementation('org.junit.jupiter:junit-jupiter:X.X.X')

where X.X.X is the version you want. For example in my case it was

testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')

like image 4
Dagnogo Jean-François Avatar answered Oct 21 '22 21:10

Dagnogo Jean-François