java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnno

解决Spring Boot测试报NoSuchMethodError: getMergedAnnotation
博客讲述了在不使用maven管理jar包的旧项目中遇到的Spring Boot测试错误:java.lang.NoSuchMethodError。作者通过分析错误定位到spring-boot-test jar包版本过高导致的问题,并通过降低该版本解决了兼容性问题。排查思路包括检查junit、spring-test、spring-boot-test和spring core之间的版本兼容性,并在mvnrepository.com上查找jar包信息。

错误如下
由于项目比较老,未使用maven进行jar包管理,spring jar包兼容出错
降低spring-boot-test jar包版本后恢复

排查思路:

  1. 看报错,没找到spring core的getMergedAnnotation这个方法
    再看调用它的jar包是spring-boot-test,调用spring-boot-test的是spring-test。
  2. 故主要是考虑junit、spring-test、spring-boot-test、spring core的兼容性,可以去https://mvnrepository.com/下载和查看jar包发布时间
  3. 先从最后没找到的spring core开始,版本是2016年的,和其他的几个时间相近,然后是spring-boot-test,是最新的2022年jar包,故判断问题极可能出现在这个包。果然,降低版本后不再报错

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getAnnotation(SpringBootTestContextBootstrapper.java:263)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getProperties(SpringBootTestContextBootstrapper.java:258)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processPropertySourceProperties(SpringBootTestContextBootstrapper.java:231)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getAndProcessPropertySourceProperties(SpringBootTestContextBootstrapper.java:206)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:134)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:374)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305)
at org.springframework.test.context.DefaultTestContext.(DefaultTestContext.java:67)
at org.springframework.test.context.TestContextManager.(TestContextManager.java:103)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:124)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:115)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值