{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:
Breaking apart the fluent method call above will help you understand how this works.
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
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
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.
The {@link oajr.mock2.MockRest} class provides the following methods for creating requests:
For HTTP methods that pass a request body (i.e.
All other body object types are converted to strings using the
A common tactic is to override a bean's
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
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
.