198

我尝试在 Android Studio 中运行参数化单元测试,如下所示。

import android.test.suitebuilder.annotation.SmallTest;  

import junit.framework.TestCase;    

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;  

import java.util.Arrays;
import java.util.Collection;    

@RunWith(Parameterized.class)
@SmallTest
public class FibonacciTest extends TestCase {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                {0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}
        });
    }   

    @Parameter // first data value (0) is default
    public /* NOT private */ int fInput;    

    @Parameter(value = 1)
    public /* NOT private */ int fExpected; 

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.calculate(fInput));
    }
}

结果是错误,说明没有测试运行。但是,如果我删除了参数化,并将它们更改为单独测试。有用。任何人都可以阐明为什么这不起作用?Android开发还不支持参数化单元测试吗?谢谢!

下面是堆栈跟踪的错误。

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> No tests found for given includes: [com.example.......FibonacciTest]
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:testDebug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:310)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:68)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:90)
    at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:54)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:41)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:49)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72)
    at org.gradle.util.Swapper.swap(Swapper.java:38)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:47)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:71)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50)
    at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:246)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
Caused by: org.gradle.api.GradleException: No tests found for given includes: [com.example........FibonacciTest]
    at org.gradle.api.internal.tasks.testing.NoMatchingTestsReporter.afterSuite(NoMatchingTestsReporter.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:87)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:31)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy46.afterSuite(Unknown Source)
    at org.gradle.api.internal.tasks.testing.results.TestListenerAdapter.completed(TestListenerAdapter.java:48)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:87)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:31)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy45.completed(Unknown Source)
    at org.gradle.api.internal.tasks.testing.results.StateTrackingTestResultProcessor.completed(StateTrackingTestResultProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.results.AttachParentTestResultProcessor.completed(AttachParentTestResultProcessor.java:52)
    at org.gradle.api.internal.tasks.testing.processors.TestMainAction.run(TestMainAction.java:51)
    at org.gradle.api.internal.tasks.testing.detection.DefaultTestExecuter.execute(DefaultTestExecuter.java:75)
    at org.gradle.api.tasks.testing.Test.executeTests(Test.java:527)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:226)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:219)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:208)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:589)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:572)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
    ... 57 more
BUILD FAILED
Total time: 4.153 secs
No tests found for given includes: [com.example......FibonacciTest]
4

32 回答 32

337

添加到您的 build.gradle:

test {
    useJUnitPlatform()
}
于 2019-08-01T13:06:05.407 回答
326

如果您使用的是 JUnit 5+,请确保@Test从正确的库中导入注释:

import org.junit.jupiter.api.Test

不是

import org.junit.Test

于 2019-10-15T17:12:30.827 回答
176

我正在使用 JUnit 4,对我有用的是将“Gradle -> Run Tests Using”的 IntelliJ 设置从“Gradle(默认)”更改为“IntelliJ IDEA”。

在此处输入图像描述

我的修复来源:https ://linked2ev.github.io/devsub/2019/09/30/Intellij-junit4-gradle-issue/

于 2020-02-17T21:06:33.550 回答
40

要添加到Przemek315提供的已经很棒且简单的解决方案,如果您使用 Kotlin DSL,请使用相同的配置:

tasks.test {
    useJUnitPlatform()
}
于 2019-09-26T21:52:55.227 回答
15

找到了在 Android Studio 中运行测试的方法。显然使用 Gradle Configuration 运行它不会执行任何测试。相反,我使用 JUnit 配置。这样做的简单方法是转到选择要运行的测试类并右键单击。然后选择运行。之后,您将看到 2 个运行选项。根据图像选择底部的(JUnit)在此处输入图像描述

(注意:如果找不到要选择的 2 Run Configuration,则需要先删除之前使用的 Configuration(Gradle Configuration)。这可以通过单击顶部的“Select Run/Debug Configuration”图标来完成工具栏。

于 2015-05-29T06:24:58.340 回答
13

添加

test {
    useJUnitPlatform()
}

到顶层给了我错误“>找不到参数的方法test()”。相反,我不得不将它添加到块testOptionsandroid

android {
    ...

    testOptions {
        unitTests.all {
            useJUnitPlatform()
        }
    }
}
于 2021-08-26T08:18:25.823 回答
13

我犯了这样定义我的测试的错误

class MyTest {
    @Test
    fun `test name`() = runBlocking {


        // something here that isn't Unit
    }
}

这导致runBlocking返回一些东西,这意味着该方法不是 void 并且 junit 没有将其识别为测试。那很蹩脚。我现在明确提供一个类型参数来运行阻塞。它不会停止疼痛或让我恢复两个小时,但它会确保这种情况不会再次发生。

class MyTest {
    @Test
    fun `test name`() = runBlocking<Unit> { // Specify Unit


        // something here that isn't Unit
    }
}
于 2021-01-06T22:54:59.590 回答
11

如果您正在使用 intellij 并想使用 gradle,则需要将其添加到 build.gradle 文件的依赖项部分:

testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
于 2020-07-17T17:02:14.873 回答
7

您可以检查您的方法是否是私有的。花了很多时间来修复这个愚蠢的错误......

于 2020-10-19T13:54:09.060 回答
7

Kotlin DSL:添加到您的 build.gradle.kts

tasks.withType<Test> {
        useJUnitPlatform()
}

Gradle DSL:添加到您的 build.gradle

test {
    useJUnitPlatform()
}
于 2020-10-04T11:13:15.653 回答
6

以防万一这对任何人都有帮助,这在 IntelliJ 上发生在我身上,情况是,测试之前一直在使用 Gradle,但突然间,第二天一些测试停止工作,给出了这个错误。

我所做的是:

  1. 转到首选项 -> 构建、执行、部署 -> Gradle -> 将“运行测试使用”更改为“IntelliJ IDEA”。
  2. 运行你的测试。
  3. 再次转到首选项 -> 构建、执行、部署 -> Gradle -> 将“运行测试使用”更改为“Gradle(默认)”。
  4. 继续运行您的测试,它现在正在运行。

这样做,我避免了更改本地配置或更改项目中的 Gradle 文件。

在此处输入图像描述

于 2021-11-14T16:52:01.660 回答
6

对于遇到错误的任何人:Expression 'test' cannot be invoked as a function添加建议的解决方案时,请尝试

tasks.withType<Test> {
    useJUnitPlatform()
}

适用于 2021 年的 KMM 设置。

于 2021-09-27T14:31:18.933 回答
4

加入 build.gradle,

test {
    useJUnitPlatform()
}

并为@Test使用类org.junit.jupiter.api.Test,而不是org.junit.Test

于 2021-08-10T16:00:16.487 回答
3

我最近遇到了这个问题,经过深入研究,我发现我正在使用库中的@Test注释junit。但是,我的build.gradle文件声明了另一个测试库。

因此,当我使用现有的测试库@Test中的注释时,它就起作用了。build.gradle

于 2021-09-22T05:06:29.280 回答
3

对我来说,错误消息的原因

没有找到给定包括的测试

无意中.java在我的src/test/kotlin测试目录下添加了一个测试文件。将文件移动到正确的目录后src/test/java,测试再次按预期执行。

于 2020-08-19T07:09:07.303 回答
2

我在 gradle 构建依赖列表中添加了一个字符串:

dependencies {
    implementation("org.junit.jupiter:junit-jupiter-params:5.8.0")
    testImplementation(kotlin("test"))
}

重建gradle,一切正常。

并记得添加测试文件:

import org.junit.jupiter.params.ParameterizedTest
于 2021-09-15T14:21:21.280 回答
2

就我而言,问题是在使用 Kotlin 编写并使用 IDEA 2020.3 时发生的。尽管 build.gradle.kts 中有正确的条目。原来问题出在IDEA IDE(Alt + Insert)生成测试函数的时候。它生成以下代码:

@Test
   internal fun name () {
     TODO ("Not yet implemented")
   }

删除“内部”修饰符后问题将得到解决:

@Test
   fun name () {
     TODO ("Not yet implemented")
   }
于 2020-12-07T21:49:12.790 回答
1

对我来说,当我添加@EnableJUnit4MigrationSupport类注释时它起作用了。

(当然还有已经提到的 gradle 库和设置)

于 2020-12-13T16:49:50.673 回答
1

我的设置是 Android Studio,Junit 4,使用 robolectric 进行测试。

问题是我将此noverify设置添加到应用程序级别 build.gradle 以尝试Java.lang.VerifyError: Bad return type从具有代码覆盖率的 Android Studio 运行测试中解决。(https://github.com/robolectric/robolectric/issues/3023

在此处输入图像描述

删除它可以解决问题。

于 2021-09-04T00:48:20.680 回答
1

对于 JUnit 5,我们应该使用 importorg.junit.jupiter.api.Test并且应该使用@ExtendWith而不是@RunWith.

于 2021-05-14T09:00:21.943 回答
1

脚步:

  1. 在 build.gralde
    => testCompile 组中添加平台运行器:'org.junit.platform',名称:'junit-platform-runner',版本:'1.7.0'
  2. 使用 @RunWith => @RunWith(JunitPlatform.class) 注释测试类
于 2020-11-21T16:35:26.313 回答
1

允许通过命令或 UI 使用 Intellij IDEA 对所有子模块运行测试。

subprojects {
    withType<Test> {
        useJUnitPlatform()
    }
}
于 2021-04-09T22:58:20.067 回答
0

当我使用junit5时,它正在工作。但是每次我执行gradle --clean时,我都会 Class not found出错。然后我将它添加到build.gradle以解决我的问题,我仍然可以使用 junit4:

test {
}
于 2020-11-18T06:48:38.793 回答
0

在我的情况下,由于 Junit 的运行时错误,我收到此消息,这在 gradle 测试任务执行的输出中根本不可见。我遇到这个有几个原因:

  1. 包括org.junit.platform:junit-platform-launcher版本与我使用的 junit 版本不匹配的依赖项
  2. META-INF/services有一个我已经注释掉的 Junit 测试监听器的条目

您可以尝试重新运行--debug并搜索FAILEDor org.gradle.api.internal.tasks.testing.TestSuiteExecutionException。在我的第二种情况下,例外是:

2020-10-20T11:34:26.517-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.517-0700 [DEBUG] [TestEventLogger] Gradle Test Executor 1 STARTED
2020-10-20T11:34:26.661-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger] Gradle Test Executor 1 FAILED
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]     org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 1.
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:63)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at java.lang.reflect.Method.invoke(Method.java:498)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at com.sun.proxy.$Proxy2.stop(Unknown Source)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:132)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.lang.reflect.Method.invoke(Method.java:498)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:413)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.lang.Thread.run(Thread.java:748)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]         Caused by:
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]         java.util.ServiceConfigurationError: org.junit.platform.launcher.TestExecutionListener: Provider com.example.myproject.MyCommentedOutClass not found
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader.fail(ServiceLoader.java:239)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.lang.Iterable.forEach(Iterable.java:74)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:94)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:67)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:97)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             ... 25 more

请注意,这些是DEBUG日志。我没有看到任何有用的东西--info

于 2020-10-20T18:39:16.070 回答
0

我也面临同样的问题。就我而言,我正在使用带有 gradle 6.6 的 JUnit 5。我在一个单独的文件夹调用integ中管理集成测试用例。我必须在build.gradle文件中定义一个新任务,在添加第一行 -> 之后useJUnitPlatform(),我的问题就解决了

于 2020-09-28T00:44:04.577 回答
0

我使用包@Test的注释 org.junit.Test,但我遇到了同样的问题。添加后testImplementation("org.assertj:assertj-core:3.10.0")build.gradle它工作。

于 2020-11-01T17:10:51.263 回答
0

我使用的是私有方法。将访问修饰符更改为 public 对我有帮助。

于 2021-08-13T11:47:04.040 回答
-1

它有助于添加:测试 {useJUnitPlatform()} 但您还需要为我准备最近的 springBootVersion 它与“2.4.5”一起使用

于 2021-06-21T19:41:56.603 回答
-1

就我而言,我在 Intellij 中运行我的测试课程并看到No tests were found. 我已将我的测试方法标记为static- 删除这解决了我的问题。

于 2021-10-02T17:26:32.170 回答
-1

没有一个答案对我有用。我需要添加 robolectric 依赖项来解决这个问题。

于 2021-06-30T08:38:24.630 回答
-2

在 Kotlin 中编写@ParameterizedTest方法,我必须在我的build.gradle文件中添加以下依赖项:

dependencies {
   testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

显然,JUnit 5 将单元测试 API 和运行时引擎分开了,您需要将两者都包括在内,以便它能够进行单元测试。

于 2021-10-04T16:00:24.447 回答
-2

只是添加......也许会帮助别人......

就我而言,这个问题是由于单元测试的命名空间中的大小写差异而发生的。一个字母是大写而不是小写(camelCase 而不是 camelcase)。

于 2021-10-01T17:02:28.567 回答