{new} 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.
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.
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:
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.