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.
A Gradle plugin that allows for the execution of JUnit 5 tests in Android environments using Android Gradle Plugin 4.0. 0 or later.
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.
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.
JUnit 5 tests will run with Java 8 or above.
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.
If you want to use JUnit 5 in your instrumented tests (androidTest source set) do the following:
androidTestImplementation("de.mannodermaus.junit5:android-test-core:1.3.0")
androidTestRuntimeOnly("de.mannodermaus.junit5:android-test-runner:1.3.0")
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)
}
}
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
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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With