繁体   English   中英

无法使用Spring Boot Gradle插件通过依赖项管理执行junit5测试

[英]Failed to execute junit5 tests with dependency management with spring boot gradle plugin

在基于gradle和kotlin的项目中,我有一些子模块。 为它构建配置模块(build.gradle.kts):

plugins {
    java
    kotlin("jvm")
    id("org.springframework.boot") version "2.1.5.RELEASE"
    id("io.spring.dependency-management") version "1.0.7.RELEASE"
}

dependencies {

    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31")
    compile("org.springframework.boot:spring-boot-starter-web")

    testCompile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31")
    testCompile("org.jetbrains.kotlin:kotlin-stdlib:1.3.31")
    testCompile("org.jetbrains.kotlin:kotlin-reflect:1.3.31")

    testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}

我尝试在此模块中运行一些简单的测试

简单类:

class Hello {
    fun hello(): String {
        return "hello"
    }
}

简单测试:

class HelloTest {
    @Test
    fun test() {
        Assertions.assertEquals(Hello().hello(), "hello")
    }
}

当我尝试运行测试时出现错误:

Jun 12, 2019 2:58:31 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
    at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
    at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
    at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:102)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:82)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:78)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
    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 org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.stop(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:132)
    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 org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:175)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:157)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:748)

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:rest:test'.
> No tests found for given includes: [ua.company.app.Hello](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
4 actionable tasks: 2 executed, 2 up-to-date
No tests found for given includes: [ua.company.app.Hello](filter.includeTestsMatching)

但是,如果从我的构建配置中删除id(“ io.spring.dependency-management”)版本“ 1.0.7.RELEASE” ,并将使用像这样的spring boot版本compile(“ org.springframework.boot:spring-boot-starter -web:2.1.5.RELEASE“)测试正确运行。

Spring依赖管理破坏了我的junit5测试。 请谁能解释为什么会这样以及如何解决问题? 谢谢!

似乎添加Spring Dependency Management会更改junit-jupiter依赖项的某些传递依赖项。

您声明的junit版本(5.4.2)取决于junit-platform-commons 1.4.2,但是当您添加Spring Dependency Management时,它会将commons版本从1.4.2更改为1.3.2。 这就是为什么您收到NoSuchMethodError的原因,因为该方法是在第四版中添加的!

我可以想到以下两个解决方案:

1)(我建议这样做)删除声明的junit版本,并让Spring Dependency Management处理junit的所有内容:

testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntime("org.junit.jupiter:junit-jupiter-engine")

2)声明junit的传递依赖版本(如果这是必不可少的,这将允许您继续使用版本5.4.2)

testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testImplementation("org.junit.platform:junit-platform-commons:1.4.2")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.4.2")
testRuntime("org.junit.platform:junit-platform-engine:1.4.2")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM