【问题标题】:JerseyTest NoSuchMethodErrorJerseyTest NoSuchMethodError
【发布时间】:2016-03-06 12:54:19
【问题描述】:

我尝试在球衣上运行任何测试后收到以下堆栈跟踪(这只发生在 junit 测试上):

java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:331)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:311)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:337)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:140)
at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:82)
at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:66)
at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory.create(GrizzlyTestContainerFactory.java:130)
at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我的 Maven 依赖项如下所示:

  <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <version.jdk>1.7</version.jdk>
 <version.mvn.compiler>3.2</version.mvn.compiler>
 <version.mvn.war.plugin>2.6</version.mvn.war.plugin>
 <jaxrs.version>2.0.1</jaxrs.version>
 <version.jersey>2.22.2</version.jersey>
 <version.servlet.api>3.0.1</version.servlet.api>

<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>${jaxrs.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${version.jersey}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${version.jersey}</version>
    </dependency>
    <!-- Testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.test-framework</groupId>
        <artifactId>jersey-test-framework-core</artifactId>
        <scope>test</scope>
        <version>${version.jersey}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
        <version>${version.jersey}</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <scope>compile</scope>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>          
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
    </dependency>
</dependencies>

我的基础测试类看起来像:

public class BaseResourceTest extends JerseyTest{
@Override
protected Application configure() {
    ResourceConfig rc = new ResourceConfig();
    rc.packages("com.someName.jaxrs.resource");
    AbstractBinder binder = new AbstractBinder() {
        @Override
        protected void configure() {
            bindFactory(MockDataServiceFactory.class).to(DataService.class);
        }
    };
    rc.register(binder);
    rc.register(AuthenticationFilter.class);
    rc.register(PermissionFilter.class);
    return rc;
    }
 }

我的测试类看起来像这样:

public class SomeResourceTests extends BaseResourceTest{
     @Test
    public void testSomething() {
        System.out.println("test"); // the exception occures in the base class so this print doesn't happen
    }
}

感谢您的帮助!

【问题讨论】:

  • 我可以从你提供的信息告诉你,这个问题是不可重现的。问题正如@Hermant 所提到的,但您提供的任何内容都不会导致这种情况。也许尝试清洁和构建。也许您之前有冲突的依赖项,将其删除并且从未清理过,我不知道

标签: java maven junit jersey jersey-test-framework


【解决方案1】:

java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

这是由于 jar 冲突导致的,这意味着您的某些类存在于不同的 jar 中但在同一个包中。 由于您仅在测试用例中遇到此问题,因此请再次扫描您的 jar。

【讨论】:

    【解决方案2】:

    我之前也遇到过同样的问题。您的类路径中可能有两个不同版本的 javax.ws.rs-api 依赖项。您明确包含的一个依赖项。另一个将从其他依赖项传递过来。 只需进入 pom.XML 的依赖层次结构(在 Eclipse 中)。找出包含 javax.WS.rs-api 依赖项的工件并在您的依赖项中添加排除项。

    【讨论】:

    • 嗨,感谢您的帮助,但它不起作用,我为每个项目添加了一个排除标签,除了 javax.ws.rs 依赖项本身,我仍然收到相同的错误
    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 2014-12-06
    • 2014-11-12
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多