Serverless Unit Testing

The {@link oajr.mock.MockRest} class is used for performing serverless unit testing of REST interfaces.

Example:

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

The {@link oajrc.RestClientBuilder#mockHttpConnection(MockHttpConnection)} method is used to associate a MockRest with a RestClient to allow for serverless testing of clients.

Example:

@Test public void testClient() throws Exception { MockRest mr = MockRest.create(MyRest.class); RestClient rc = RestClient.create().mockHttpConnection(mr).build(); assertEquals("'foo'", rc.doPut("/String", "'foo'").getResponseAsString()); }

Mocked connections can also be used for serverless testing of remote resources and interfaces.

Example:

// Our remote resource to test. @RemoteResource public interface MyRemoteInterface { @RemoteMethod(httpMethod="GET", path="/echoQuery") public int echoQuery(@Query(name="id") int id); } // Our mocked-up REST interface to test against. @RestResource public class MyRest { @RestMethod(name=GET, path="/echoQuery") public int echoQuery(@Query("id") String id) { return id; } } @Test public void testProxy() { MockRest mr = MockRest.create(MyRest.class); MyRemoteInterface r = RestClient .create() .mockHttpConnection(mr) .build() .getRemoteResource(MyRemoteInterface.class); assertEquals(123, r.echoQuery(123)); }