java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass

1. Issue Description

When I run a JUnit test under spring context, come across this error.

Test code looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring.xml")
public class MollySessionRunner {
	@Autowired
	MSession session;
	
	@Test
	public void runSession(){
	    session.request("");
	}
}
Admittedly, this is not a real test, I use it just for handy manual test.

The error:

java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
         at org.springframework.test.context.ContextLoaderUtils.resolveContextLoader(ContextLoaderUtils.java:109)
         at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:362)
         at org.springframework.test.context.TestContext.<init>(TestContext.java:100)
         at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:118)
         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:119)
         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:108)
         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
         at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
         at java.lang.reflect.Constructor.newInstance(Unknown Source)
         at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
         at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
         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:26)
         at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
         at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
         at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
         at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
         at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
         at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
         at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
         at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2. Analysis and Solution

I googled and got some valuable posts, like this one: http://stackoverflow.com/questions/425039/no-such-method-error-when-creating-junit-test. 

Cite:The java.lang.NoSuchMethodError always indicates that the version of a class that was on your compiler's classpath is different from the version of the class that is on your runtime classpath (had the method been missing at compile-time, the compile would have failed.)

Then, I went to check my dependency jars, one finding is: the latest spring-data-redis 1.0.4 depends on spring 3.1.4, however, the latest spring framework is 3.2.2, therefore, I removed my spring 3.2.2 dependency in POM and used the spring-data-redis 1.0.4 's including spring 3.1.4, but, no joy.

Another try is, I removed almost all dependencies, still no joy, which drove me crazy somehow.

Finally, I remembered my project depends on another maven project, I added that project in Java Build Path-> Projects. That project has some different dependencies with mine, I removed that project then the error was gone.

3. Go further

Since we don't add project dependency in Java Build Path-> Projects, then how to depend it?

First, I executed 'maven:install' in that project, and deleted the maven folder including a pom.xml in the exported jar file, I added the jar file in Java Build Path-> Libraries -> Add External JARs. This means solved the NoSuchMethodError, however, a new error occurred, 

java.io.FileNotFoundException: file:\D:\xas-common-0.0.1.jar!\mo.properties
The subproject needs to read a .properties file, after exported as a .jar, it couldn't read it any more, as file in a jar has a different path, including a '!'.

Final solution, add the dependency by maven,

<dependency>
	<groupId>com.derek</groupId>
	<artifactId>xas-common</artifactId>
	<version>0.0.1</version>
</dependency>
I don't know the reason under surface yet, but it works, no dependency collisions, no file read errors. I will dig the maven later.



### 解决 Java 中 Spring 框架下的 `NoSuchMethodError` 异常 当遇到 `java.lang.NoSuchMethodError: org.springframework.util.Assert.notNull(Ljava/lang/Object;)V` 这样的错误时,通常是因为项目中的不同模块使用了不兼容的 Spring 版本。这种情况下,某些类的方法签名可能发生了变化,而旧版本的字节码仍然被加载。 为了确保所有依赖项都匹配当前使用的 Spring Framework 主版本号,建议采取以下措施: #### 1. 统一管理依赖版本 通过 Maven 或 Gradle 的 BOM (Bill of Materials) 文件来统一控制整个项目的 Spring 库版本。这可以防止子模块引入不同的次要或补丁级别版本[^1]。 对于 Maven 用户来说,在 pom.xml 中加入如下配置: ```xml <dependencyManagement> <dependencies> <!-- 使用官方发布的最新稳定版BOM --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springboot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` #### 2. 明确指定测试库版本 如果确实需要单独调整某个特定组件(如 spring-test),则应显式声明其版本号,并保持与其他核心库一致[^3]: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> ``` #### 3. 清理本地仓库缓存 有时即使更新了 POM 文件,由于本地 .m2 存储目录内残留的历史 jar 包影响编译结果。此时可以通过删除对应路径下的文件夹强制重新下载所需资源。 #### 4. 验证运行环境一致性 确认开发工具、构建服务器以及 CI/CD 流程所处环境中安装的 JDK 和其他外部插件均处于相同状态,避免因环境差异引发潜在问题。 以上方法能够有效减少由版本冲突引起的各种异常情况的发生概率。当然,具体实施还需结合实际情况灵活运用。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值