Home > Java > javaTutorial > body text

Springboot unit test depends on pit example source code analysis

王林
Release: 2023-05-23 19:01:29
forward
1306 people have browsed it

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.(TestContextManager.java:124)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:142)
    at org.springframework.test.context.junit4.SpringRunner.(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

Springboot unit test depends on pit example source code analysis

再往下深入,检查carrier-api依赖

Springboot unit test depends on pit example source code analysis

幸运的是,问题的根源迅速被发现,是由于spring-web的引用导致的错误。将该引用注释掉,并且将相关代码进行引用替换。

problem solved

Springboot unit test depends on pit example source code analysis

The above is the detailed content of Springboot unit test depends on pit example source code analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template