java 未找到给定的测试包括:[***Test](filter.includeTestsMatching)

uidvcgyl  于 2023-05-21  发布在  Java
关注(0)|答案(9)|浏览(166)

我正在使用 Intellij 2019.2 运行gradle测试用例,但是,我一直收到以下异常。根据一些帖子,我做了以下几点:
1.将junit(junit-jupiter-api:5.3.1)依赖项添加为testImplementation
1.将useJUnitPlatform()放入build.gradle文件
1.设置Intellij的Gradle runner以使用Gradle(默认)来运行我的测试用例。
执行上述步骤后,问题仍然存在。
有人能给予我一些提示如何摆脱这个错误吗?真的很感激

Caused by: org.gradle.api.tasks.testing.TestExecutionException: No tests found for given includes: [***Test](filter.includeTestsMatching)
                at org.gradle.api.internal.tasks.testing.NoMatchingTestsReporter.afterSuite(NoMatchingTestsReporter.java:37)
                at jdk.internal.reflect.GeneratedMethodAccessor307.invoke(Unknown Source)
                at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
                at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
                at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:42)
                at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:230)
                at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:149)
                at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:58)
                at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:324)
                at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:234)
                at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:140)
                at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:37)
                at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
                at com.sun.proxy.$Proxy99.afterSuite(Unknown Source)
                at org.gradle.api.internal.tasks.testing.results.TestListenerAdapter.completed(TestListenerAdapter.java:48)
                at jdk.internal.reflect.GeneratedMethodAccessor325.invoke(Unknown Source)
                at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
                at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
                at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:42)
                at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:230)
                at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:149)
                at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:58)
                at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:324)
                at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:234)
                at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:140)
                at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:37)
                at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
                at com.sun.proxy.$Proxy101.completed(Unknown Source)
                at org.gradle.api.internal.tasks.testing.results.StateTrackingTestResultProcessor.completed(StateTrackingTestResultProcessor.java:96)
                at org.gradle.api.internal.tasks.testing.results.AttachParentTestResultProcessor.completed(AttachParentTestResultProcessor.java:56)
                at org.gradle.api.internal.tasks.testing.processors.TestMainAction.run(TestMainAction.java:60)
                at org.gradle.api.internal.tasks.testing.detection.DefaultTestExecuter.execute(DefaultTestExecuter.java:116)
                at org.gradle.api.internal.tasks.testing.detection.DefaultTestExecuter.execute(DefaultTestExecuter.java:51)
                at org.gradle.api.tasks.testing.AbstractTestTask.executeTests(AbstractTestTask.java:483)
                at org.gradle.api.tasks.testing.Test.executeTests(Test.java:587)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:103)
                at org.gradle.api.internal.project.taskfactory.StandardTaskAction.doExecute(StandardTaskAction.java:48)
                at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:41)
                at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:28)
                at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:705)
                at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:672)
                at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$4.run(ExecuteActionsTaskExecuter.java:338)
                at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:402)
                at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:394)
                at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
                at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
                at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
                at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:92)
                at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
                at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:327)
                at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:312)
                at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.access$200(ExecuteActionsTaskExecuter.java:75)
                at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$TaskExecution.execute(ExecuteActionsTaskExecuter.java:158)
                at org.gradle.internal.execution.impl.steps.ExecuteStep.execute(ExecuteStep.java:46)
                at org.gradle.internal.execution.impl.steps.CancelExecutionStep.execute(CancelExecutionStep.java:34)
                at org.gradle.internal.execution.impl.steps.TimeoutStep.executeWithoutTimeout(TimeoutStep.java:69)
                at org.gradle.internal.execution.impl.steps.TimeoutStep.execute(TimeoutStep.java:49)
                at org.gradle.internal.execution.impl.steps.CatchExceptionStep.execute(CatchExceptionStep.java:34)
                at org.gradle.internal.execution.impl.steps.CreateOutputsStep.execute(CreateOutputsStep.java:49)
                at org.gradle.internal.execution.impl.steps.SnapshotOutputStep.execute(SnapshotOutputStep.java:42)
                at org.gradle.internal.execution.impl.steps.SnapshotOutputStep.execute(SnapshotOutputStep.java:28)
                at org.gradle.internal.execution.impl.steps.CacheStep.executeWithoutCache(CacheStep.java:133)
                at org.gradle.internal.execution.impl.steps.CacheStep.lambda$execute$5(CacheStep.java:83)
                at org.gradle.internal.execution.impl.steps.CacheStep.execute(CacheStep.java:82)
                at org.gradle.internal.execution.impl.steps.CacheStep.execute(CacheStep.java:37)
                at org.gradle.internal.execution.impl.steps.PrepareCachingStep.execute(PrepareCachingStep.java:33)
                at org.gradle.internal.execution.impl.steps.StoreSnapshotsStep.execute(StoreSnapshotsStep.java:38)
                at org.gradle.internal.execution.impl.steps.StoreSnapshotsStep.execute(StoreSnapshotsStep.java:23)
                at org.gradle.internal.execution.impl.steps.SkipUpToDateStep.executeBecause(SkipUpToDateStep.java:95)
                at org.gradle.internal.execution.impl.steps.SkipUpToDateStep.lambda$execute$0(SkipUpToDateStep.java:88)
                at org.gradle.internal.execution.impl.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:52)
                at org.gradle.internal.execution.impl.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:36)
                at org.gradle.internal.execution.impl.DefaultWorkExecutor.execute(DefaultWorkExecutor.java:34)
                at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:109)
                ... 129 more```
hjzp0vay

hjzp0vay1#

我不确定这是否有帮助,但我将测试类上的import语句从import org.junit.Test更改为import org.junit.jupiter.api.Test,这似乎解决了这个问题。

fxnxkyjh

fxnxkyjh2#

就加

tasks.withType(Test) {
    useJUnitPlatform()
}

到您的应用
build.gradle > root
文件。在我的情况下,我有以下日志记录:

tasks.withType(Test) {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}
xvw2m8pv

xvw2m8pv3#

升级到Android Studio Chipmunk后面临此问题。通过从模块的build.gradle中删除以下代码解决了此问题:

tasks.withType(Test) {
    useJUnitPlatform()
}

看起来此配置是解决Android Studio(或AGP)早期版本上此问题的方法之一,但由于一些新的更改,它本身成为了问题的根源。

ngynwnxp

ngynwnxp4#

我也有同样的问题。在调试模式--debug模式下运行gradle后,控制台包含org.junit.platform.commons.PreconditionViolationExceptionClassNotFoundException
通过添加以下内容修复了问题:

testImplementation 'org.junit.platform:junit-platform-commons:1.5.2'
huwehgph

huwehgph5#

我也面临着同样的问题。最初,我在Gradle项目中有2个依赖项,因为我遇到了这个问题。前两个依赖关系如下:

compile "org.codehaus.groovy:groovy-all:2.5.8"
testCompile "org.spockframework:spock-core:1.1-groovy-2.4"

然后我添加了第三个依赖项,解决了这个问题。

testCompile platform("org.spockframework:spock-bom:2.0-M1-groovy-2.5")
z0qdvdin

z0qdvdin6#

正是公开了所有的测试函数才解决了我的问题。

oprakyz7

oprakyz77#

此异常的原因之一是在Java中使用了Default访问修饰符。
JUnit测试方法应避免以下情况:声明为static、未声明为public、不返回void或具有参数。
这样的测试方法很容易在无意中创建,但JUnit测试运行程序不会执行它们。
因此,与此相反:

@Test
void test() {
   assertEquals(1, 1);
}

添加public如下:

@Test
public void test() {
   assertEquals(1, 1);
}
ou6hu8tu

ou6hu8tu8#

我今天在我们的一个项目中遇到了这个错误。经过几分钟的努力,我发现问题出在我使用的JUnit版本上。在迁移到JUnit版本'5.8.1'之后,我的所有测试都停止运行,所以我回滚到了版本5.3.1

u5rb5r59

u5rb5r599#

在我的例子中,这个问题是由Robolectric 4.8引起的,它不支持targetSdkVersion 33。升级到Robolectric 4.9解决了这个问题。通过在测试类而不是方法上运行测试,我能够注意到这个问题,这给出了targetSdkVersion=33 > maxSdkVersion=32错误消息。

相关问题