java.lang.NoClassDefFoundError:org/springframework/test/context/TestContextAnnotationUtils
我正在Gradle项目Java11中使用Junit5和SpringBoot2设置单元测试。我被这个错误缠住了。我在网上尝试了一种可能的解决方案,但无法找出原因 Gradle和Spring引导版本:java.lang.NoClassDefFoundError:org/springframework/test/context/TestContextAnnotationUtils,java,spring-boot,unit-testing,gradle,junit5,Java,Spring Boot,Unit Testing,Gradle,Junit5,我正在Gradle项目Java11中使用Junit5和SpringBoot2设置单元测试。我被这个错误缠住了。我在网上尝试了一种可能的解决方案,但无法找出原因 Gradle和Spring引导版本: @ExtendWith(SpringExtension.class) //@ContextConfiguration(classes = {HealthCheckService.class}) //@WebMvcTest @AutoConfigureMockMvc @SpringBootTe
@ExtendWith(SpringExtension.class)
//@ContextConfiguration(classes = {HealthCheckService.class})
//@WebMvcTest
@AutoConfigureMockMvc
@SpringBootTest
@Tag("healthCheck")
class HealthCheckServiceTest {
@Autowired
private MockMvc mockMvc;
@InjectMocks
private HealthCheckService healthCheckService;
}
类路径
'org.springframework.boot:springboot gradle插件:2.1.8.RELEASE'
分配=https://services.gradle.org/distributions/gradle-5.6.4-bin.zip
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
是在SpringFramework 5.3.0的SpringTest
模块中引入的
由于类路径中缺少TestContextAnnotationUtils
,因此OverrideAutoConfiguration ContextCustomizerFactory
(来自Spring Boot Test)失败,因此您似乎拥有Spring框架中较新版本的Spring Boot和较旧版本的模块(例如,Spring Test
)
通常,您在构建中混合了许多可能不兼容的依赖项(例如,各种JUnit平台1.5.2/1.6.1和JUnit Jupiter 5.6.0工件)
当使用SpringBoot时,您不应该使用这样的显式版本声明依赖项。相反,您应该依赖SpringBoot的支持来管理版本。有关详细信息,请参阅此答案:
如果看不到一个最小的可复制的例子,我们只能说。。。请清理依赖关系管理并依靠Spring Boot来管理版本。是否可以提供一个?很难说漏掉了什么。到目前为止,我看到的问题是:混合使用testCompile
和testimplention
,spring引导测试自动配置是compile
范围,而不是testImplementation
,我认为根本不应该提供,spring上下文
似乎是多余的,因为您正在使用spring引导。。。
// 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'