简体   繁体   English

java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils

[英]java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils

I am setting unit tests with Junit5 and Spring Boot 2 in my Gradle project JAVA 11. I am stuck with this error.我在我的 Gradle 项目 JAVA 11 中使用 Junit5 和 Spring Boot 2 设置单元测试。我遇到了这个错误。 I have tried a possible solution available online but am not able to figure out what is causing this.我已经尝试了一种可能的在线解决方案,但无法弄清楚是什么原因造成的。

Gradle and Spring boot version: Gradle 和 Spring 启动版本:

classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE' distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-bin.zip类路径'org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE'distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-bin.zip

Test ignored.

java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils

at org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory.createContextCustomizer(OverrideAutoConfigurationContextCustomizerFactory.java:41)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.getContextCustomizers(AbstractTestContextBootstrapper.java:404)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:376)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:312)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:265)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:98)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:139)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$0(ExtensionValuesStore.java:81)
at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:182)
at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:84)
at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:88)
at org.junit.jupiter.engine.execution.NamespaceAwareStore.getOrComputeIfAbsent(NamespaceAwareStore.java:61)
at org.springframework.test.context.junit.jupiter.SpringExtension.getTestContextManager(SpringExtension.java:202)
at org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(SpringExtension.java:76)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$7(ClassBasedTestDescriptor.java:359)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:359)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:189)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:78)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:132)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
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:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Suppressed: java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils
    at org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory.createContextCustomizer(OverrideAutoConfigurationContextCustomizerFactory.java:41)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.getContextCustomizers(AbstractTestContextBootstrapper.java:404)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:376)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:312)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:265)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:98)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:139)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$0(ExtensionValuesStore.java:81)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:182)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.remove(ExtensionValuesStore.java:98)
    at org.junit.jupiter.engine.execution.NamespaceAwareStore.remove(NamespaceAwareStore.java:73)
    at org.springframework.test.context.junit.jupiter.SpringExtension.afterAll(SpringExtension.java:88)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeAfterAllCallbacks$13(ClassBasedTestDescriptor.java:425)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeAfterAllCallbacks$14(ClassBasedTestDescriptor.java:425)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeAfterAllCallbacks(ClassBasedTestDescriptor.java:425)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.after(ClassBasedTestDescriptor.java:213)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.after(ClassBasedTestDescriptor.java:78)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:145)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:145)
    ... 27 more



 Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.TestContextAnnotationUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 51 more

Test Class:测试 Class:

 @ExtendWith(SpringExtension.class)
 //@ContextConfiguration(classes = {HealthCheckService.class})
 //@WebMvcTest
 @AutoConfigureMockMvc
 @SpringBootTest
 @Tag("healthCheck")
 class HealthCheckServiceTest {

      @Autowired
      private MockMvc mockMvc;
      @InjectMocks
      private HealthCheckService healthCheckService;

 }

build.gradle build.gradle

// FOR TESTS
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude group: "org.junit.vintage", module: "junit-vintage-engine"
        exclude group: "junit", module: "junit"
    }
    compile 'org.springframework.boot:spring-boot-test-autoconfigure:2.4.2'
    testCompile "org.assertj:assertj-core:3.16.1"
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.6.0'
    testCompile ('org.junit.jupiter:junit-jupiter:5.6.0')
    implementation group: 'org.springframework', name: 'spring-context', version: '5.3.3'
    testImplementation 'org.junit.platform:junit-platform-commons:1.5.2'
    testImplementation 'org.junit.platform:junit-platform-launcher:1.6.1'
    testCompile 'org.mockito:mockito-junit-jupiter:3.5.7'

TestContextAnnotationUtils was introduced in Spring Framework 5.3.0 in the spring-test module. TestContextAnnotationUtilsspring-test模块中的 Spring Framework 5.3.0 中引入。

Since OverrideAutoConfigurationContextCustomizerFactory (from Spring Boot Test) fails due to the absence of TestContextAnnotationUtils in the classpath, it appears that you have a more recent version of Spring Boot and older versions of modules from the Spring Framework (eg, spring-test ). Since OverrideAutoConfigurationContextCustomizerFactory (from Spring Boot Test) fails due to the absence of TestContextAnnotationUtils in the classpath, it appears that you have a more recent version of Spring Boot and older versions of modules from the Spring Framework (eg, spring-test ).

In general, you are mixing a number of potentially incompatible dependencies in your build (for example, the various JUnit Platform 1.5.2/1.6.1 and JUnit Jupiter 5.6.0 artifacts).通常,您在构建中混合了许多可能不兼容的依赖项(例如,各种 JUnit Platform 1.5.2/1.6.1 和 JUnit Jupiter 5.6.0 工件)。

When using Spring Boot, you should not be declaring dependencies with explicit versions like that.当使用 Spring Boot 时,您不应该使用这样的显式版本声明依赖关系。 Rather, you should rely on Spring Boot's support for managing the versions.相反,您应该依靠 Spring Boot 对管理版本的支持。 See this answer for details: Gradle 5 JUnit BOM and Spring Boot Incorrect Versions有关详细信息,请参阅此答案: Gradle 5 JUnit BOM 和 Spring 引导不正确的版本

Without seeing a Minimal Reproducible Example , we cannot help much more than to say... please clean up your dependency management and rely on Spring Boot to manage the versions.如果没有看到最小可重现示例,我们只能说......请清理您的依赖管理并依靠 Spring Boot 来管理版本。

暂无
暂无

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

相关问题 java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils 在 SOAP web 服务测试 - java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils in SOAP web service test java.lang.NoClassDefFoundError:org / springframework / web / context / ContextCleanupListener - java.lang.NoClassDefFoundError: org/springframework/web/context/ContextCleanupListener java.lang.NoClassDefFoundError:org / springframework / context / EnvironmentAware - java.lang.NoClassDefFoundError: org/springframework/context/EnvironmentAware 弹簧 java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext - SPRING java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven - java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven java.lang.NoClassDefFoundError:org / springframework / web / servlet / HttpServletBean:org / springframework / context / EnvironmentAware - java.lang.NoClassDefFoundError: org/springframework/web/servlet/HttpServletBean : org/springframework/context/EnvironmentAware 初始化上下文时java.lang.NoClassDefFoundError:org.springframework.beans.FatalBeanException - java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException when initializing context Spring-Security :java.lang.NoClassDefFoundError: org/springframework/security/context/DelegatingApplicationListener - Spring- Security :java.lang.NoClassDefFoundError: org/springframework/security/context/DelegatingApplicationListener 引起:java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallableProcessingInterceptor - Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallableProcessingInterceptor 发生 JNI 错误,线程“main”中的异常 java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext - A JNI error has occurred, Exception in thread “main” java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM