{8.1.0-new} MockRest

The {@link oajr.mock2.MockRest} class is used for performing serverless unit testing of {@link oajr.annotation.Rest @Rest}-annotated classes. These include both parent resource classes that extend from {@link oajr.RestServlet} and child resources that do not.

The API consists of the following classes:

The following shows a simple example of invoking a PUT method on a simple REST interface and asserting the correct status code and response body:

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

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.build(EchoRest.class); // Create a request. MockServletRequest req = mr.put("/echo", "'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 concept of the design is simple. The {@link oajr.mock2.MockRest} class is used to create instances of {@link oajr.mock2.MockServletRequest} and {@link oajr.mock2.MockServletResponse} which are passed directly to the call handler on the resource class {@link oajr.RestCallHandler#service(HttpServletRequest,HttpServletResponse)}. In effect, you're fully testing your REST API as if it were running in a live servlet container, yet not actually having to run in a servlet container.

The create(Object) method can take in either Class objects or pre-instantiated beans. The latter is particularly useful for testing Spring beans.


By default, the {@link oajr.mock2.MockRest} class specifies the following request headers:

Accept: application/json+simple Content-Type: application/json

The reason for using "application/json+simple" as the default is that it significantly simplifies testing by allowing you to compare response content with simple Java strings without having to escape lots of quotes:

// Using Simple JSON mr.assertBody("{foo:'bar':baz:123}"); // Using normal JSON mr.assertBody("{\"foo\":\"bar\",\"baz\":123}");

Other media types headers can be specified via any of the following methods:

Various other convenience methods for common media types are also provided.

The following examples are functionally equivalent for specifying XML serialization:

MockRest mr; mr = MockRest.build(EchoRest.class, Xml.DEFAULT); mr = MockRest.build(EchoRest.class, XmlSerializer.DEFAULT, XmlParser.DEFAULT); mr = MockRest.create(EchoRest.class).marshall(Xml.DEFAULT).build(); mr = MockRest.create(EchoRest.class).serializer(XmlSerializer.DEFAULT).parser(XmlParser.DEFAULT).build(); mr = MockRest.create(EchoRest.class).accept("text/xml").contentType("text/xml").build(); mr = MockRest.create(EchoRest.class).xml().build();


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

For HTTP methods that pass a request body (i.e. PUT,POSTPATCH), the body object can be any of the following types:

All other body object types are converted to strings using the toString() method.

A common tactic is to override a bean's toString() method to return Simple JSON so that instances can be passed to the methods above.

public class MyBean { ... @Override public String toString() { SimpleJson.DEFAULT.toString(this); } }

The {@link oajr.mock2.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.mock2.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.mock2.MockRest} class has a debug mode that will cause your HTTP requests and responses to be sent to the console:

MockRest mr = MockRest .create(MyRest.class) .debug() .simpleJson() .build();