JUnit 5 for Android 測試扩展答案

2022-09-02 12:37:31

如何配置JUnit 5进行安卓单元测试?我试过了:

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

但是它不起作用,当我运行之前最简单的单元测试时:

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

我收到错误:

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.

答案 1

请参阅此问题以及有关 Android 上的 JUnit 5 支持的此问题

将此构建脚本依赖项添加到顶级 build.gradle[.kts] 文件中:

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

并将这些行添加到您的应用程序或库 build.gradle[.kts] 文件中:

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

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

如果您使用的是 Android Gradle 插件 3.2.0 或更高版本以及 Gradle 4.7 或更高版本,则可以使用上述解决方案。有关更多信息,请参阅插件 GitHub 页面

扩展答案

如果要在检测的测试(androidTest 源代码集)中使用 JUnit 5,请执行以下操作:

  1. 将这些依赖项添加到您的应用程序或 libray 构建脚本中:
androidTestImplementation("de.mannodermaus.junit5:android-test-core:1.3.0")
androidTestRuntimeOnly("de.mannodermaus.junit5:android-test-runner:1.3.0")
  1. 在测试类中使用代替:ActivityScenarioExtensionActivityScenarioRule
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)
  }
}

答案 2

Android Studio是基于IDEA的,对吗?

然后在此处查看此答案 https://stackoverflow.com/a/46161799/1431016 或直接阅读 http://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-idea

总结:

// 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")

我还没有尝试过这个项目,但它可以帮助你在Android上使用JUnit 5:https://github.com/aurae/android-junit5


推荐