我正在Gradle项目Java11中使用Junit5和SpringBoot2设置单元测试。我被这个错误缠住了。我在网上尝试了一种可能的解决方案,但无法找出原因。
Gradle和Spring启动版本:
classpath'org.springframework.boot: sping-boo-gradle-plugin: 2.1.8.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
测试等级:
@ExtendWith(SpringExtension.class)
//@ContextConfiguration(classes = {HealthCheckService.class})
//@WebMvcTest
@AutoConfigureMockMvc
@SpringBootTest
@Tag("healthCheck")
class HealthCheckServiceTest {
@Autowired
private MockMvc mockMvc;
@InjectMocks
private HealthCheckService healthCheckService;
}
建筑格拉德尔
// 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
是在SpringFramework 5.3中引入的。Spring测试模块中的0。
由于OverrideAutoConfigurationContextCustomizerFactory
(来自Spring Boot Test)由于类路径中缺少TestContextAnnotationUtils
而失败,因此您似乎拥有Spring Boot的最新版本和Spring框架中的旧版本模块(例如,Spring)-test
)。
通常,您在构建中混合了许多可能不兼容的依赖项(例如,各种JUnit平台1.5.2/1.6.1和JUnit Jupiter 5.6.0工件)。
当使用SpringBoot时,您不应该使用这样的显式版本声明依赖项。相反,您应该依赖SpringBoot的支持来管理版本。有关详细信息,请参阅此答案:Gradle 5 JUnit BOM和Spring Boot版本不正确
如果没有看到最小可复制示例,我们只能说...请清理您的依赖项管理,并依靠Spring Boot来管理版本。
JUnit是一个Regression Testing Framework ,开发人员使用它来实现Java中的单元测试,并加快编程速度并提高代码质量。 JUnit Framework可以轻松地与以下任何一个集成 - Eclipse Ant Maven JUnit测试框架的特性 JUnit测试框架提供以下重要功能 - Fixtures 测试套件 测试跑步者 JUnit类 Fixtures Fixtur
我使用Spock框架和Groovy脚本来测试我的Java应用程序。对于我创建的所有类,我的项目有100%的测试覆盖率。然而,我注意到覆盖范围显示,名为“app”的主类没有得到充分测试。 这是我对App类的一个测试: 如何用Spock框架充分测试Spring Boot应用程序上下文?
本文向大家介绍Spring框架实现文件上传功能,包括了Spring框架实现文件上传功能的使用技巧和注意事项,需要的朋友参考一下 在Java中实现文件的上传有多种方式,如smartUpload或是使用Strus2,本文与大家分享使用Spring框架中的MultipartFile类来实例文件的上传。 不啰嗦了,直接上干货。先是编写了一个实现文件上传的类FileUploadingUtil,此类中定义了两
我正在为OSGi(又名Eclipse Gemini Blueprint)工作到Spring。
本文向大家介绍spring框架下websocket的搭建,包括了spring框架下websocket的搭建的使用技巧和注意事项,需要的朋友参考一下 本文基于Apach Tomcat 8.0.3+MyEclipse+maven+JDK1.7 spring4.0以后加入了对websocket技术的支持,撸主目前的项目用的是SSM(springMVC+spring+MyBatis)框架,所以肯定要首选s
测试乃企业级软件开发的重要组成部分之一。本章专注于讲解采用 IoC 原则进行编码而给单元测试带来的好处,以及 Spring 框架对集成测试的支持如何为测试带来帮助。(对企业开发中如何进行代码测试的详尽讨论不在本文档讨论范围之内)