1. Issue Description
When I run a JUnit test under the spring context, the 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 isn't a real test and I use the 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.s Pringframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration (contextloaderutils.java:362) at O Rg.springframework.test.context.testcontext.<init> (testcontext.java:100) at Org.springframework.test.conte Xt. Testcontextmanager.<init> (testcontextmanager.java:118) at Org.springframework.test.context.junit4.SpringJU Nit4classrunner.createtestcontextmanager (springjunit4classrunner.java:119) at ORG.SPRINGFRAMEWORK.TEST.CONTEXT.J Unit4. Springjunit4classrunner.<init> (springjunit4classrunner.java:108) at Sun.reflect.NativeConstructorAccessorI
Mpl.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.AllDefaultP Ossibilitiesbuilder.runnerforclass (alldefaultpossibilitiesbuilder.java:26) at Org.junit.runners.model.RunnerBuil Der.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: ) at ORG.ECLIPSE.JDt.internal.junit4.runner.JUnit4TestLoader.createTest (junit4testloader.java:48) at Org.eclipse.jdt.internal.juni T4.runner.JUnit4TestLoader.loadTests (junit4testloader.java:38) at Org.eclipse.jdt.internal.junit.runner.RemoteTe Strunner.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:3
) 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 "version of" of "a", "was" your compiler ' s classpath is Different from the version of the class this is on your runtime classpath (had the method been missing at Compile-time, T He 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, Howe Ver, the latest spring framework is 3.2.2, therefore, I removed me 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 are, 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 this project in Java build path-> Projects. That is project has some different dependencies with mine, I removed that project then the error is gone.
3. Go further
Since we don ' t add Project dependency in Java builds path-> Projects, then how to depend it?
I executed ' Maven:install ' in that project, and deleted the Maven folder including a pom.xml in the exported Jar fi Le, 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 no 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'll dig the maven later.