38

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)
Robert Brooks
  • 701
  • 2
  • 8
  • 12
  • Is this context.xml file complete? And can you try running the sample with a stable version of spring rather than a RC version? I guess `ContextLoaderUtils` is a new class so it is hard to trace the error. – melihcelik Nov 23 '11 at 13:08
  • I'm having the same issue. Workes when I do mvn clean install.. but fails in STS with the same error.. something's up with 3.1.0 RC – user48545 Dec 06 '11 at 02:51

6 Answers6

59

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

Jason
  • 7,356
  • 4
  • 41
  • 48
3

If solutions above are not working, it might be eclipse's fault: in eclipse, on the project -> properties -> java build path -> Librairies -> if there is "Junit 4", remove it.

I had this stacktrace:

java.lang.NoSuchMethodError: org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runLeaf(Lorg/junit/runners/model/Statement;Lorg/junit/runner/Description;Lorg/junit/runner/notification/RunNotifier;)V
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)

And of course updating the pom wasn't the solution...

Asoub
  • 2,273
  • 1
  • 20
  • 33
  • Unfortunately, the error still remais after I did this... Any suggestions? – Johannes Jul 24 '17 at 13:37
  • @Johannes clean/build the project, even close it, then close eclipse and reopen them after. Have you tried the other answers ? Mostly this is a dependency issue, so you'll have to check if they are well resolved. Check if you don't have a dependency on another project that still references junit 4. Maybe put a breakpoint where the error pop up and check which jar is really used. – Asoub Jul 24 '17 at 15:43
  • thanks for your continously attention on this. I managed to resolve this problem by removing all dependencies from my POM, and then re-adding them via Maven from IntelliJ compiler suggestions, and then importing these newly added classes in my java classes. Somehow, the problem was solved this way... Thanks mate – Johannes Aug 03 '17 at 14:21
1

In my case,I had dependency one junit-dep and one junit, when I remove the junit-dep, everything is ok.

feng smith
  • 1,487
  • 2
  • 9
  • 22
0

add this one to your pom File, I had the same ERROR, but I have done this, now it works for me,

          <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.1.0.RC1</version>
</dependency>


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.0.RELEASE</version>
</dependency>

Note: I have POM parent, and POM child, and I wanted to test my child project, but I got error. mvn test.... so I have done this thing. in Parent.

with version and in child without. like this one org.springframework spring-beans

            <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
</dependency>

try it.

Ibo
  • 89
  • 4
0

It complains about java.lang.NoSuchMethodError and I think this has to do with your spring dependencies. Try changing your spring dependencies like this:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-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>spring-core</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-asm</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.1.0.RC1</version>
    </dependency>

And I don't know where following dependencies come from, but I think they might need correction with their artifactId as well:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.transaction</artifactId>
        <version>3.1.0.RC1</version>
    </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.mongodb</groupId>
        <artifactId>mongodb-java-driver</artifactId>
        <version>2.7.2</version>
    </dependency>

And one final thing that I want you to consider, do you really, really need ALL of those dependencies?

melihcelik
  • 4,531
  • 1
  • 21
  • 25
  • 1
    Hey thanks for the help, I ended up using springframework version 3.0.6 and the error went away. Thanks for the help. FYI that was just a test spring.xml my project will use mongoDB, so I needed the majority of the dependencies. – Robert Brooks Nov 27 '11 at 05:39
0

Jay is correct. Based on what he said and the error originating from org.springframework.beans, review your dependency graph for said project and resolve any version conflicts by excluding transitive dependencies on org.springframework.beans and explicitly stating a dependency on the latest or the correct version of org.springframework.beans.

The same should probably be done to other dependencies, but if it ain't broke don't fix it ;) unless you can't get passed your OCD, like myself.

I leveraged Netbeans by selecting the project, right click-> show dependency. Once in the dependency graph view, search for Beans. Double click on the dependency and right click to resolve version conflict where a wizard like window will provide additional configurations to resolve the conflict. Piece of cake!

Thanks Jay!

mosgjig
  • 537
  • 1
  • 9
  • 18