Serverless Unit Testing

The {@link oajr.mock.MockRest} class is a simple yet powerful interface for creating serverless unit tests for your REST interfaces.

The following shows a self-encapsulated standalone JUnit testcase that tests the functionality of a simple REST interface.

Example:

public class MockTest { // Our REST resource to test. @RestResource(serializers=JsonSerializer.Simple.class, parsers=JsonParser.class) public static class MyRest { @RestMethod(name=PUT, path="/String") public String echo(@Body String b) { return b; } } @Test public void testEcho() throws Exception { MockRest.create(MyRest.class).put("/String", "'foo'").execute().assertStatus(200).assertBody("'foo'")); } }

The API consists of the following classes:

The concept of the design is simple. The {@link oajr.mock.MockRest} class is used to create instances of {@link oajr.mock.MockServletRequest} and {@link oajr.mock.MockServletResponse} which are passed directly to the call handler on the resource class {@link oajr.RestCallHandler#service(HttpServletRequest,HttpServletResponse)}.

Breaking apart the fluent method call above will help you understand how this works.

@Test public void testEcho() throws Exception { // Instantiate our mock. MockRest mr = MockRest.create(MyRest.class); // Create a request. MockServletRequest req = mr.put("/String", "'foo'"); // Execute it (by calling RestCallHandler.service(...) and then returning the response object). MockServletResponse res = req.execute(); // Run assertion tests on the results. res.assertStatus(200); res.assertBody("'foo'"); }

The {@link oajr.mock.MockRest} class provides the following methods for creating requests:

The {@link oajr.mock.MockServletRequest} class provides default implementations for all the methods defined on the {@link javax.servlet.http.HttpServletRequest} in addition to many convenience methods.

The following fluent convenience methods are provided for setting common Accept and Content-Type headers.

The following fluent convenience methods are provided for building up your request.

Fluent setters are provided for all common request headers:

The {@link oajr.mock.MockServletResponse} class provides default implementations for all the methods defined on the {@link javax.servlet.http.HttpServletResponse} in addition to many convenience methods.

The {@link oajr.mock.MockRest} object can also be used with the {@link oajrc.RestClient} class to perform serverless unit testing through the client API of REST resources. This can be useful for testing of interface proxies against REST interfaces (described later).

The example above can be rewritten to use a mock as follows:

Example:

public class MockTest { // Our REST resource to test. @RestResource(serializers=JsonSerializer.Simple.class, parsers=JsonParser.class) public static class MyRest { @RestMethod(name=PUT, path="/String") public String echo(@Body String b) { return b; } } @Test public void testEcho() throws Exception { MockRest mr = MockRest.create(MyRest.class); RestClient rc = RestClient.create().mockHttpConnection(mr).build(); assertEquals("'OK'", rc.doPut("/String", "'OK'").getResponseAsString()); } }

The {@link oajrc.RestClientBuilder#mockHttpConnection(MockHttpConnection)} method allows you to pass in a mocked interface for creating HTTP requests through the client interface. The method creates a specialized HttpClientConnectionManager for handling requests by taking information on the client-side request and populating the {@link oajr.mock.MockServletRequest} and {@link oajr.mock.MockServletResponse} objects directly without involving any sockets.