ws

JAX-RS tests

  • MyResourceService.java
  • MyResourceServiceTest.java
  • pom.xml

MyResourceService.java

package com.training.rs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/resource")
public class MyResourceService {

    @GET
    public String getServiceName() {
        return "MyResourceService";
    }

}

MyResourceServiceTest.java

package com.training.rs;

import java.io.IOException;
import java.net.URI;

import javax.ws.rs.core.UriBuilder;

import org.glassfish.grizzly.http.server.HttpServer;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;

public class MyResourceServiceTest {

    private URI uri = UriBuilder.fromUri("http://localhost/").port(8080)
            .build();
    private HttpServer server;

    @Before
    public void setUp() throws IllegalArgumentException, NullPointerException,
            IOException {
        ResourceConfig rc = new PackagesResourceConfig("com.training.rs");
        server = GrizzlyServerFactory.createHttpServer(uri, rc);
    }

    @After
    public void tearDown() {
        server.stop();
    }

    @Test
    public void test() {
        Client client = Client.create();

        WebResource webResource = client.resource(uri + "resource");

        Assert.assertEquals("MyResourceService", webResource.get(String.class));
    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.training</groupId>
    <artifactId>jax_rs_tests</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>

    <dependencies>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.18.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.18.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-grizzly2</artifactId>
            <version>1.18.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey.jersey-test-framework</groupId>
            <artifactId>jersey-test-framework-core</artifactId>
            <version>1.18.3</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.18.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

    </dependencies>

</project>