springboot单元测试依赖踩坑记录
springboot单元测试依赖踩坑
在进行springboot 单元测试的时候,发现如下错误。
java.lang.IllegalStateException: Could not load CacheAwareContextLoaderDelegate [class org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
at org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:103)
at org.springframework.test.context.BootstrapUtils.createBootstrapContext(BootstrapUtils.java:72)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatinGConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
at com.intellij.junit4.JUnit4ideaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]: Unresolvable class definition; nested exception is java.lang.NoSuchMethodError: org.springframework.core.KotlinDetector.isKotlinReflectPresent()Z
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:132)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:152)
at org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:100)
... 20 more
Caused by: java.lang.NoSuchMethodError: org.springframework.core.KotlinDetector.isKotlinReflectPresent()Z
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:171)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:122)
... 22 more
一天以前单测是能跑的,初步预计是依赖的问题。但是因为项目是多模块结构,模块与模块之间的引用比较多,需要排查每一个模块是否有问题。
如下步骤
- 1. 在出现问题的单测归属模块的pom文件中,找到所有模块间依赖
- 2. 依次到各个模块进行单元测试,定位问题模块
- 3. 排查问题模块的依赖问题
一番折腾,发现问题模块 carrier-api
再往下深入,检查carrier-api依赖
很幸运,一眼就发现了问题所在, 这里的spring-WEB的引用导致了错误的发生。将该引用注释掉,并且将相关代码进行引用替换。
问题解决
总结
1. spring-boot-starter-test 依赖包含了junit ,springboot 单测的话建议使用该依赖,包含了很多东西
2. 使用spring-boot-test 的话, 需要单独引入 spring-test, junit 依赖,否则无法单测
3. 无论是1还是2. 他们都和 spring-web 依赖冲突, 只要引入这个依赖,单测就无法运行
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关文章