使用JerseyTest 2.0时未注入@Context WebConfig

Mic*_*ley 1 integration-testing jersey-2.0

我有一个简单的资源,如:

@Path("/")
public class RootResource {
    @Context WebConfig wc;

    @PostConstruct 
    public void init() {
        assertNotNull(wc);
    }

    @GET
    public void String method() {
        return "Hello\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图使用JerseyTest(2.x,而不是1.x)和GrizzlyTestContainerFactory.

我无法解决在配置方面我需要做什么以获取注入的WebConfig对象.

小智 5

我通过创建GrizzlyTestContainerFactory的子类并显式加载Jersey servlet来解决这个问题.这会触发WebConfig对象的注入.代码如下所示:

public class ExtendedGrizzlyTestContainerFactory implements TestContainerFactory {

    private static class GrizzlyTestContainer implements TestContainer {

        private final URI uri;
        private final ApplicationHandler appHandler;
        private HttpServer server;
        private static final Logger LOGGER = Logger.getLogger(GrizzlyTestContainer.class.getName());

        private GrizzlyTestContainer(URI uri, ApplicationHandler appHandler) {
            this.appHandler = appHandler;
            this.uri = uri;
        }

        @Override
        public ClientConfig getClientConfig() {
            return null;
        }

        @Override
        public URI getBaseUri() {
            return uri;
        }

        @Override
        public void start() {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(Level.INFO, "Starting GrizzlyTestContainer...");
            }

            try {
                this.server = GrizzlyHttpServerFactory.createHttpServer(uri, appHandler);

                // Initialize and register Jersey Servlet
                WebappContext context = new WebappContext("WebappContext", "");
                ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
                registration.setInitParameter("javax.ws.rs.Application", 
                        appHandler.getConfiguration().getApplication().getClass().getName());
                // Add an init parameter - this could be loaded from a parameter in the constructor
                registration.setInitParameter("myparam", "myvalue");
                registration.addMapping("/*");
                context.deploy(server);

            } catch (ProcessingException e) {
                 throw new TestContainerException(e);
            }
        }

        @Override
        public void stop() {
             if (LOGGER.isLoggable(Level.INFO)) {
                 LOGGER.log(Level.INFO, "Stopping GrizzlyTestContainer...");
            }
            this.server.stop();
        }
    }

    @Override
    public TestContainer create(URI baseUri, ApplicationHandler application) throws IllegalArgumentException {
         return new GrizzlyTestContainer(baseUri, application);
    }
Run Code Online (Sandbox Code Playgroud)

请注意,Jersey Application组件是从ApplicationHandler加载的,它使用内部Application对象的类名称作为参数传入(ResourceConfig是Application的子类).因此,您还需要创建ResourceConfig的子类,以使此方法起作用.这个代码非常简单:

package com.example;

import org.glassfish.jersey.server.ResourceConfig;

public class MyResourceConfig extends ResourceConfig {

    public MyResourceConfig() {
        super(MyResource.class);
    }

} 
Run Code Online (Sandbox Code Playgroud)

这假设您正在测试的资源是MyResource.您还需要在测试中覆盖几个方法,如下所示:

public class MyResourceTest extends JerseyTest {

    public MyResourceTest() throws TestContainerException {

    }

    @Override
    protected Application configure() {
        return new MyResourceConfig();
    }

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

    @Test
    public void testCreateSimpleBean() {
        final String beanList = target("test").request().get(String.class);
        Assert.assertNotNull(beanList);
    }

}
Run Code Online (Sandbox Code Playgroud)

最后,为了完整性,这里是MyResource的代码:

@Path("test")
public class MyResource {

    @Context WebConfig wc;

    @PostConstruct 
    public void init() {
        System.out.println("WebConfig: " + wc);
        String url = wc.getInitParameter("myparam");
        System.out.println("myparam = "+url);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Collection<TestBean> createSimpleBean() {
        Collection<TestBean> res = new ArrayList<TestBean>();
        res.add(new TestBean("a", 1, 1L));
        res.add(new TestBean("b", 2, 2L));
        return res;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public TestBean roundTrip(TestBean s) {
        return s;
    }
}
Run Code Online (Sandbox Code Playgroud)

运行测试的输出显示WebConfig已加载,init参数现在可用:

WebConfig: org.glassfish.jersey.servlet.WebServletConfig@107d0f44
myparam = myvalue
Run Code Online (Sandbox Code Playgroud)