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

來源:互聯網
上載者:User

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 {@AutowiredMSession session;@Testpublic 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.



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.