用于 Android 测试的 JUnit 5

mac*_*229 9 java junit android unit-testing junit5

如何为 Android 单元测试配置 JUnit 5?我试过:

testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0")
Run Code Online (Sandbox Code Playgroud)

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

@Test
public void addition_isCorrect() throws Exception {
    assertEquals(4, 2 + 2);
}
Run Code Online (Sandbox Code Playgroud)

我得到错误:

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.
Run Code Online (Sandbox Code Playgroud)

Mah*_*zad 11

顶级 build.gradle文件中添加此构建脚本依赖

buildscript {
  dependencies {
    classpath "de.mannodermaus.gradle.plugins:android-junit5:1.7.1.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

并在您的应用 build.gradle文件中添加这些行:

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

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

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


扩展答案

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

  1. 将这些依赖项添加到您的应用程序构建脚本中:
androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.2.2"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.2"
Run Code Online (Sandbox Code Playgroud)
  1. 在您的测试类中使用ActivityScenarioExtension而不是ActivityScenarioRule
buildscript {
  dependencies {
    classpath "de.mannodermaus.gradle.plugins:android-junit5:1.7.1.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

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


Sor*_*ras 4

Android Studio是基于IDEA的,对吗?

然后在此处查看此答案/sf/answers/3231325961/或直接阅读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")
Run Code Online (Sandbox Code Playgroud)

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