How to set servlet context in Jersey Unit Test Framework 2.5

I have two questions:

1) How to set the servlet context for a JerseyTesttest that extends JerseyTest. From my research, it seems that I need to create TestContainerfor TestFactoryand pass on AppDescriptor, but it seems more complex than it should be. Are there any other suggestions there?

In general, I was looking for a way to set the servlet context in the unit test of my Jersey resource class, which is usually run by 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 have a controller layout so that I can pass it into context.

2) What is the difference between using a jersey test from

<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 a jersey frame 2.5

+4
2

-, .

: :

StatusService JSON.

:

pom.xml

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Jersey Test Framework for Jersey service tests -->
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
            <version>2.26</version>
            <scope>test</scope>
        </dependency>

StatusServiceJerseyTest.java

public class StatusServiceJerseyTest {
    private static TestContainer container = null;

    @BeforeClass
    public static void configure() {
        Assume.assumeTrue(container == null);
        GrizzlyWebTestContainerFactory factory = 
                new GrizzlyWebTestContainerFactory();
        container = factory.create(
                UriBuilder.fromUri("http://localhost/").port(0).build(),
                configureDeployment());
        container.start();
    }

    @AfterClass
    public static void destroy() {
        Assume.assumeNotNull(container);
        container.stop();
    }

    /**
     * Necessary to configure Jersey Test Framework with a
     * ServletContextListener.
     */
    private static DeploymentContext configureDeployment() {
        return ServletDeploymentContext
                .forServlet(new ServletContainer(new ResourceConfig()
                        .packages(StatusService.class.getPackage().getName())))
                .build();
    }

    @Test
    public void testStatusNoParam() {
        Assume.assumeNotNull(container);
        String baseUri = container.getBaseUri().toString();
        String jsonResponse = ClientBuilder.newClient().target(baseUri)
                .path("status")
                .request(MediaType.APPLICATION_JSON).get(String.class);

        Assert.assertNotNull(jsonResponse);
        Assert.assertFalse(jsonResponse.isEmpty());
    }
}
0

org.springframework.mock.web.MockServletContext . Spring.

MockServletContext

-1

Source: https://habr.com/ru/post/1525958/


All Articles