Michael J Lawley
Michael J Lawley

Reputation: 140

@Context WebConfig not injected when using JerseyTest 2.0

I have a simple resource like:

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

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

    @GET
    public void String method() {
        return "Hello\n";
    }
}

Which I am trying to use with JerseyTest (2.x, not 1.x) and the GrizzlyTestContainerFactory.

I can't work out what I need to do in terms of config to get the WebConfig object injected.

Upvotes: 1

Views: 1684

Answers (2)

Michael J Lawley
Michael J Lawley

Reputation: 140

The solution from @ametke worked well but wasn't picking up my ExceptionMapper classes. To solve this I simplified the start() method to:

@Override
public void start() {
  try {
    initParams.put("jersey.config.server.provider.packages", "my.resources;my.config");
    this.server = GrizzlyWebContainerFactory.create(uri, initParams);
  } catch (ProcessingException | IOException e) {
    throw new TestContainerException(e);
  }
}

This was based on Problems running JerseyTest when dealing with HttpServletResponse

Upvotes: 0

ametke
ametke

Reputation: 86

I solved this issue by creating a subclass of GrizzlyTestContainerFactory and explicitly loading the Jersey servlet. This triggers the injection of the WebConfig object. The code looks like this:

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);
    }

Notice that the Jersey Servlet configuration is being loaded from the ApplicationHandler that is passed in as a parameter using the inner Application object's class name (ResourceConfig is a subclass of Application). Therefore, you also need to create a subclass of ResourceConfig for this approach to work. The code for this is very simple:

package com.example;

import org.glassfish.jersey.server.ResourceConfig;

public class MyResourceConfig extends ResourceConfig {

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

} 

This assumes the resource you are testing is MyResource. You also need to override a couple of methods in your test like this:

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);
    }

}

Finally, for completeness, here is the code for 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;
    }
}

The output of running the test shows that the WebConfig is loaded and the init param is now available:

WebConfig: org.glassfish.jersey.servlet.WebServletConfig@107d0f44
myparam = myvalue

Upvotes: 5

Related Questions