Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JUnit Test Error

Tags:

spring

junit4

I am receiving the following error when I attempt to run my Spring JUnit test. I am just trying to get familiar with creating JUnits using the Spring Framework.

JUnit Class:

package org.xxx.springdao.mongo_datadictionary;  import static org.junit.Assert.*;  import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.xxx.springdao.mongo_datadictionary.SimpleSpringApp;   @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class SpringJunitTest {       @Test     public void testSpringTest() {         SimpleSpringApp test = new SimpleSpringApp();         assertNotNull("Success", test);     }  } 

SpringJunitTest-context.xml:

<pre>      <?xml version="1.0" encoding="UTF-8"?>     <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:context="http://www.springframework.org/schema/context"         xmlns:mongo="http://www.springframework.org/schema/data/mongo"         xsi:schemaLocation="http://www.springframework.org/schema/context               http://www.springframework.org/schema/context/spring-context-3.0.xsd               http://www.springframework.org/schema/data/mongo               http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd               http://www.springframework.org/schema/beans               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">      </beans>  </block> 

Pom.xml:

    <dependency>         <groupId>junit</groupId>         <artifactId>junit</artifactId>         <version>4.10</version>         <scope>test</scope>     </dependency>     <dependency>         <groupId>org.springframework.data</groupId>         <artifactId>spring-data-commons-core</artifactId>         <version>1.2.0.BUILD-SNAPSHOT</version>     </dependency>     <dependency>         <groupId>org.springframework.data</groupId>         <artifactId>spring-data-mongodb</artifactId>         <version>1.0.0.M5</version>     </dependency>     <dependency>         <groupId>org.springframework.data</groupId>         <artifactId>spring-data-mongodb-cross-store</artifactId>         <version>1.0.0.M5</version>     </dependency>     <dependency>         <groupId>org.springframework.data</groupId>         <artifactId>spring-data-mongodb-log4j</artifactId>         <version>1.0.0.M5</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.beans</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.aspects</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.mongodb</groupId>         <artifactId>mongodb-java-driver</artifactId>         <version>2.7.2</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.context</artifactId>         <version>3.1.0.RC1</version>         <scope>compile</scope>         <exclusions>             <exclusion>                 <groupId>commons-logging</groupId>                 <artifactId>commons-logging</artifactId>             </exclusion>         </exclusions>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.core</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.asm</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.aop</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.expression</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.orm</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.transaction</artifactId>         <version>3.1.0.RC1</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>org.springframework.test</artifactId>         <version>3.1.0.RC1</version>     </dependency> 

Error:

Tests in error:
initializationError(org.saic.springdao.mongo_datadictionary.SpringJunitTest): org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;

Stack Trace:

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:87)     at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:298)     at org.springframework.test.context.TestContext.<init>(TestContext.java:100)     at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:117)     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(NativeConstructorAccessorImpl.java:39)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)     at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)     at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)     at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)     at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)     at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)     at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)     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) 
like image 414
Robert Brooks Avatar asked Nov 23 '11 04:11

Robert Brooks


People also ask

Why is my JUnit test failing?

Example Failure When writing unit tests with JUnit, there will likely be situations when tests fail. One possibility is that our code does not meet its test criteria. That means one or more test cases fail due to assertions not being fulfilled.

What is ApplicationContext failed to load?

However, sometimes in this situation, we may encounter the application context loading error “Failed to load ApplicationContext.” This error appears in the test classes because the application context isn't loaded in the test context.

What is @SpringBootTest?

The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.

How can you tell the difference between an unexpected exception and a failed test?

Failures are when your test cases fail – i.e. your assertions are incorrect. Errors are when unexpected errors/exceptions occur - i.e. while trying to actually run the test and an unexpected exception is thrown like FileNotFound, etc`.


1 Answers

I had this exact same issue when upgrading from Spring 3.0.6 and to 3.1.0 release. Here is the solution:

Some of your dependencies are listed as being the 3.1.0 version, but they resolve their transitive dependencies to the older versions which don't have the method listed in the error message.

If you use Eclipse, open the pom.xml and look at the dependency hierarchy tab. Some libraries you declared will be listed as version 3.1.0 but have a resolved dependency of 3.0.6. For me, I had spring-security-config and spring-security-taglibs listed as version 3.1 in my pom, but they resolved dependencies such as spring-core and spring-expression as 3.0.6.

To resolve this, explicitly list each of the highest transitive dependencies explicitly in your pom. For example, I was originally counting on spring-security-config to bring in spring-core as a transitive dependency, so I did not list the spring-core artifact in my pom. But to fix this issue I changed my pom to explicitly include spring-core (version 3.1.0). I added direct dependencies for every resolved dependency in my pom that was being resolved to an older version, and then it worked!

Hope this helps...

like image 149
Jason Avatar answered Oct 02 '22 09:10

Jason