用于 Android 测试的 JUnit 5 [英] JUnit 5 for Android testing

查看:78
本文介绍了用于 Android 测试的 JUnit 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

出现错误:

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.

推荐答案

在您的顶级 build.gradle 文件中添加此构建脚本依赖项:

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

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

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

And add these lines in your app build.gradle file:

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"
}

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

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 refere to the plugin Github page.

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

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

  1. 将这些依赖项添加到您的应用构建脚本:

androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.2.2"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.2"

  1. 在测试类中使用 ActivityScenarioExtension 而不是 ActivityScenarioRule:
  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)
  }
}

参考这个问题这个问题 关于 Android 上的 JUnit 5 支持.

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

这篇关于用于 Android 测试的 JUnit 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆