简体   繁体   English

JerseyTest在Jersey 2.13中使用GrizzlyWebTestContainerFactory

[英]JerseyTest using GrizzlyWebTestContainerFactory in Jersey 2.13

I am trying to get a JerseyTest running using a org.glassfish.jersey.test.grizzly.GrizzlyWebContainerFactory . 我正在尝试使用org.glassfish.jersey.test.grizzly.GrizzlyWebContainerFactory运行org.glassfish.jersey.test.grizzly.GrizzlyWebContainerFactory I already searched the internet and tried several things for the better part of a day. 我已经搜索了互联网,并在一天中的大部分时间里尝试了几件事。 It seems to be impossible and I would really appreciate any help on how to get this up and running. 这似乎是不可能的,我真的很感激有关如何启动和运行的任何帮助。

I created a minimal example based on the code from the Jersey 2 documentation. 我根据Jersey 2文档中的代码创建了一个最小的示例。 The code looks like: 代码如下:

package test;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.Test;

public class DistributedDeploymentTest extends JerseyTest {

@Path("hello")
public static class HelloResource {
    @GET
    public String getHello() {
        return "Hello World!";
    }
}

@Override
protected Application configure() {
    return new ResourceConfig(HelloResource.class);
}

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
    return new GrizzlyWebTestContainerFactory();
}

@Override
protected DeploymentContext configureDeployment() {
    return ServletDeploymentContext.builder(configure()).build();
}

@Test
public void testSingleNode() throws Exception {
    final String hello = target("hello").request().get(String.class);
    assertEquals("Hello World!", hello);
}
}

It works perfectly fine without the GrizzlyWebTestContainerFactory . 没有GrizzlyWebTestContainerFactory它完美无缺。 However upon running this example I always get: 但是在运行这个例子后,我总是得到:

javax.ws.rs.NotFoundException: HTTP 404 Not Found
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:956)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:795)
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:683)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:408)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:308)
    at de.tudresden.inf.rn.zeebus.DistributedDeploymentTest.testSingleNode(DistributedDeploymentTest.java:66)
    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:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    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)

I'm using the following Maven dependencies: 我正在使用以下Maven依赖项:

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.13</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework</groupId>
            <artifactId>jersey-test-framework-core</artifactId>
            <version>2.13</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
            <version>2.13</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.2.2.v20140723</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>9.2.2.v20140723</version>
        </dependency>

Not sure why it doesn't work with builder(configure()) , but if we change it to 不知道为什么它不能用于builder(configure()) ,但是如果我们将其更改为

return ServletDeploymentContext.forPackages(
                                  getClass().getPackage().getName()).build();

(as seen here ), it'll work (no need to override configure ). (如看到这里 ),它会工作(不需要重写configure )。 You can see some more of the sample test configurations in jersey-test-framework-examples 您可以在jersey-test-framework-examples中看到更多示例测试配置


EDIT 编辑

"But how do I add my custom subclass of ResourceConfig (not shown in the example above) in that case?" “但是,在这种情况下,如何添加ResourceConfig的自定义子类(上面的示例中未显示)?”

return ServletDeploymentContext.forServlet(new ServletContainer(
                               new ResourceConfig(HelloResource.class))).build();

As seen here 正如所看到的在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM