【问题标题】:Spring 4.1.7 + Junit 4.12 not working while executing from eclipse从 Eclipse 执行时 Spring 4.1.7 + Junit 4.12 不工作
【发布时间】: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.74.2.3,在这些解决方案中遇到同样的错误对我没有帮助,因为我的 pom.xml 只有 4.x 版本。

正在使用的 Eclipse Kepler 版本

【问题讨论】:

  • 请提供一个最小样本,该样本将通过尝试使用该缺失方法的执行路径。我无法在本地复制此内容。确保你正在运行你认为你正在运行的东西。
  • @SotiriosDelimanolis 我已经更新了我的问题。tnx
  • 在构建项目时尝试运行测试,如果是 maven 项目,请不要跳过测试
  • @VedantKekan 是的,它从 eclip 对我有用右键单击项目-> 运行方式-> Maven 测试。谢谢 :)
  • @VedantKekan 仍在探索一种单独运行测试的方法,得到了我在问题中提到的相同异常:(

标签: java eclipse spring maven junit


【解决方案1】:

根据 Eclipse 的配置方式,您的运行/调试配置可能在类路径中没有 Spring 库 - 将它们放在您的 POM 中并不一定足够。

检查调试配置 > {您的调试配置} > 类路径 您可能需要先在“用户条目”下手动添加它们,看看是否是问题所在

【讨论】:

  • 在类路径中,Maven Dependecies 在用户条目下可用。
【解决方案2】:

初步看来,好像是jar版本的问题。

我建议解决这个版本问题是使用BOM-Bill of Material

这将允许您跳过显式添加版本,并提供最新版本。

要使用 BOM,请将以下内容添加到您的部分:

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

然后您可以简单地声明您通常的 Spring Framework 依赖项,而无需指定:

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

试一试,希望能解决你的问题。

【讨论】:

    【解决方案3】:

    基于 Spring 文档: (https://docs.spring.io/spring/docs/4.1.7.RELEASE/spring-framework-reference/html/testing.html) 你必须将 JUnit 的版本设置在 4.9 和 4.11 之间

    检查 Spring 文档中的 JUnit 版本很重要。这个可能会改变。

    这里是 Spring 4.1.7 的解释:

    Spring TestContext 框架通过自定义运行程序提供与 JUnit 4.9+ 的完全集成(在 JUnit 4.9 — 4.11 上测试)。通过使用 @RunWith(SpringJUnit4ClassRunner.class) 注释测试类,开发人员可以实现标准的基于 JUnit 的单元和集成测试,同时获得 TestContext 框架的好处,例如支持加载应用程序上下文、测试实例的依赖注入、事务测试方法执行等等。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 2015-10-06
      • 2019-11-14
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多