【发布时间】:2016-02-18 03:57:35
【问题描述】:
我只是想在我们的 spring 应用程序中实现单元测试并获得以下异常,同时从 eclipse 执行 junit 测试。
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)
我的 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>
我执行下面的文件来获取异常。 eclipse文件中的执行步骤->右键->运行方式->Junit测试
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);
}
}
我也尝试了这两个弹簧版本4.1.7 和4.2.3,在这些解决方案中遇到同样的错误对我没有帮助,因为我的 pom.xml 只有 4.x 版本。
正在使用的 Eclipse Kepler 版本
【问题讨论】:
-
请提供一个最小样本,该样本将通过尝试使用该缺失方法的执行路径。我无法在本地复制此内容。确保你正在运行你认为你正在运行的东西。
-
@SotiriosDelimanolis 我已经更新了我的问题。tnx
-
在构建项目时尝试运行测试,如果是 maven 项目,请不要跳过测试
-
@VedantKekan 是的,它从 eclip 对我有用右键单击项目-> 运行方式-> Maven 测试。谢谢 :)
-
@VedantKekan 仍在探索一种单独运行测试的方法,得到了我在问题中提到的相同异常:(
标签: java eclipse spring maven junit