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.



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值