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)
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.propertiesThe 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.