Java - Rest

About

Specification

  • Jax Rs is an annotated Rest specification

Implementation

Test

When testing a Rest API (ie when writing a unit test), there is two approach:

  • you create a context object to emulate an incoming request
  • or you start an embedded server and use a client to make http request

Example:

public class ServerClientTest {

    private HttpServer server;
    private WebTarget target;

    @Before
    public void setUp() throws Exception {
        server = Main.startServer();

        Client c = ClientBuilder.newClient();
        target = c.target(Main.BASE_URI);
    }

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

    /**
     * Test to see that the message "Got it!" is sent in the response.
     */
    @Test
    public void testGetIt() {
        String responseMsg = target.path("myresource").request().get(String.class);
        assertEquals("Got it!", responseMsg);
    }
}

Javdoc

Framework


Powered by ComboStrap