1

Am just trying to implement unit testing in our spring application and getting below exception, while executing a junit test from eclipse.

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.isInJavaLangAnnotationPackage(Ljava/lang/annotation/Annotation;)Z
    at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor(MetaAnnotationUtils.java:117)
    at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor(MetaAnnotationUtils.java:88)
    at org.springframework.test.context.support.ActiveProfilesUtils.resolveActiveProfiles(ActiveProfilesUtils.java:80)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:367)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305)
    at org.springframework.test.context.DefaultTestContext.<init>(DefaultTestContext.java:67)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:103)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:124)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:115)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    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:33)
    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)

My pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.my.playground</groupId>
  <artifactId>sample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>sample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
  <build>
    <finalName>sample</finalName>
  </build>
</project>

I execute below file to get the exception. Execution step from eclipse file -> Right Click -> Run As -> Junit Test

package junit;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class JunitSpringExample {

    @Autowired
    private SampleService sampleService;

    @Test
    public void testSampleService() {
        // Just to make sure this method works
        Assert.assertTrue(true);
    }
}

I also tried with these two spring versions 4.1.7 and 4.2.3, getting same error in those solutions is not helping me as my pom.xml has only 4.x version.

Eclipse Kepler version am using

Community
  • 1
  • 1
prem
  • 843
  • 1
  • 8
  • 21
  • Please provide a minimal sample that will go through the execution path that would try to use that missing method. I can't reproduce this locally. Make sure you are running what you think you are running. – Sotirios Delimanolis Nov 16 '15 at 22:29
  • @SotiriosDelimanolis I have updated my question.tnx – prem Nov 17 '15 at 07:41
  • Try running tests while building your project, if its maven project please dont skip tests – VedantK Nov 17 '15 at 18:35
  • @VedantKekan yeah it worked for me from eclipe Right click on project -> Run As -> Maven Test.Thanks :) – prem Nov 21 '15 at 17:43
  • @VedantKekan still am exploring a way to run test individually , getting the same exception i mentioned in my question :( – prem Nov 21 '15 at 17:55
  • @prem please go through my answer – VedantK Nov 21 '15 at 18:14
  • I have exactly the same problem and I am sure that I have only one spring version. Can you tell me if you have solved it? – mgiammarco Apr 02 '17 at 08:38
  • I have moved away from spring version + eclipse version to latest and able work fine. – prem Sep 11 '18 at 12:21

3 Answers3

0

It's possible, depending on how Eclipse is configured, that your Run/Debug configuration doesn't have the Spring libraries on the classpath - it's not necessarily enough to have them in your POM.

Check Debug Configurations > {Your Debug Configuration} > Classpath You may need to add them manually under 'User Entries' first to see if that's the issue

Seldon75
  • 38
  • 4
0

prima facie, It seems problem of jar versions.

What I recommend to solve this version issue is use of BOM-Bill of Material

This will allow you to skip explicitly adding version, and make latest version available.

To use the BOM, add the following to your section:

<dependencyManagement> 
   <dependencies> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-framework-bom</artifactId> 
      <version>4.0.0.RC2</version> 
      <type>pom</type> 
      <scope>import</scope>
    </dependency> 
   </dependencies> 
</dependencyManagement>

You can then simply declare your usual Spring Framework dependencies without needing to specify a :

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

Just give a try, hope this solves your problem.

VedantK
  • 9,728
  • 7
  • 66
  • 71
0

Based on the Spring documentation: (https://docs.spring.io/spring/docs/4.1.7.RELEASE/spring-framework-reference/html/testing.html) you must set the version of JUnit between 4.9 and 4.11

It's important to check the JUnit version in the Spring documentation. This one may change.

Here the explanation for Spring 4.1.7 :

The Spring TestContext Framework offers full integration with JUnit 4.9+ through a custom runner (tested on JUnit 4.9 — 4.11). By annotating test classes with @RunWith(SpringJUnit4ClassRunner.class), developers can implement standard JUnit-based unit and integration tests and simultaneously reap the benefits of the TestContext framework such as support for loading application contexts, dependency injection of test instances, transactional test method execution, and so on.

Hope this help.