6

I have two questions:

1) How do you set the servlet Context for a Unit Test that extends JerseyTest. From the research I've done, it seems that I have to create a TestContainer for a TestFactory and pass in an AppDescriptor, but that seems more complicated then it should be. Is there any other suggestions out there?

In general, looking for a way to set the Servlet Context in a unit test of my Jersey Resource Class which is done normally by the web.xml.

Example:

@Path(value = "/service")
public class Foo{

@Context ServletContext ctx;

@GET
@Path(value="/list")
public String list() {
    Controller ctrl = new Controller();
    ctx.setAttribute("controller", ctrl);
    return ctrl.getList();
}

}

public class FooUnitTest extends JerseyTest
{
    @Test
    public void testService()
    {

         //set/how to configure the context?
    }
}

The goal is to mock out the Controller so I can pass it into the context.

2) What's the difference between using the jersey test from the

<dependency>
    <groupId>com.sun.jersey.jersey-test-framework</groupId>
    <artifactId>jersey-test-framework-grizzly</artifactId>
    <version>1.5-SNAPSHOT</version>
    <scope>test</scope>
</dependency>

vs.

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-bundle</artifactId>
    <version>2.4.1</version>
</dependency>

This is Jersey framework 2.5

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
darewreck
  • 2,576
  • 5
  • 42
  • 67

1 Answers1

-2

You can use org.springframework.mock.web.MockServletContext class to mock servlet context. It's a part of Spring framework.

MockServletContext