0

尝试在球衣上运行任何测试后,我收到以下堆栈跟踪(这只发生在 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
    }
}

感谢您的协助!

4

2 回答 2

0

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

于 2016-03-06T13:13:17.103 回答
0

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

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

于 2016-03-06T13:22:58.553 回答