3

I created this test in Jersey (from the docs), which works fine, with one problem: the @WebListener ServletContextListener is not being invoked.

The Resource classes that I need to test rely on an attribute set on the ServletContext by the ServletContextListener.

Can I make sure it is invoked, or can I manipulate the ServletContext in some other way?

public class SimpleTest extends JerseyTest {

    @WebListener
    public static class AppContextListener implements ServletContextListener {

        @Override
        public void contextInitialized(ServletContextEvent event) {
            System.out.println("Context initialized");
        }

        @Override
        public void contextDestroyed(ServletContextEvent event) {
            System.out.println("Context destroyed");
        }
    }

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

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

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

I added these dependencies to make this work:

<dependency>
    <groupId>org.glassfish.jersey.test-framework</groupId>
    <artifactId>jersey-test-framework-core</artifactId>
    <version>2.18</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
    <version>2.18</version>
</dependency>
wvdz
  • 16,251
  • 4
  • 53
  • 90
  • `ServletContextListener` - isn't a part JAX-RS spec, so I don't that Jersey will handle it, and especially `JerseyTest `. – Denys Denysiuk Jun 17 '15 at 16:21
  • @DenysDenysiuk Yes, it's the servlet-api. But how can I solve this problem? All the resource classes I'm testing use the ServletContext to get an attribute set in my ServletContextListener. Another way to manipulate the ServletContext would solve my problem as well. – wvdz Jun 17 '15 at 17:08
  • You can try to use [jersey listeners](https://jersey.java.net/documentation/latest/user-guide.html#d0e15535) instead of `ServletContextListener` if you need only event – Denys Denysiuk Jun 17 '15 at 17:18

1 Answers1

5

The JerseyTest needs to be set up to run in a Servlet environment, as mentioned here. Here are the good parts:

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

@Override
protected DeploymentContext configureDeployment() {
    ResourceConfig config = new ResourceConfig(SessionResource.class);
    return ServletDeploymentContext.forServlet(new ServletContainer(config))
                                   .addListener(AppContextListener.class)
                                   .build();
}

See the APIs for

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    Aaaah okay thanks. I actually was looking in the right direction, trying to make it work overriding `getTestContainerFactory()` and returning a `GrizzlyWebTestContainerFactory`, but now I have the final missing piece of the puzzle :) I got it working. – wvdz Jun 17 '15 at 22:07