{8.2.0-new} MockRestClient
The {@link oajr.mock2.MockRestClient} class is used for performing serverless unit testing of {@link oajr.annotation.Rest @Rest}-annotated and {@link oaj.http.remote.Remote @Remote}-annotated classes.
The {@link oajr.mock2.MockRestClient} itself extends from {@link oajr.client.RestClient} providing it with the rich feature set of that API. 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.MockRestClient} 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#execute(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. All aspects of the client and server side code are tested, yet no servlet container is required. The actual over-the-wire transmission is the only aspect being bypassed.
The
The {@link oajr.mock2.MockRestRequest} object has convenience methods provided to allow you to set properties directly on the underlying {@link javax.servlet.HttpServletRequest} object. The following example shows how this can be used to directly set roles on the request object to perform security testing.
The {@link oajr.mock2.MockRestClient} class has a debug mode that will cause your HTTP requests and responses to be sent to the console:
MockRestClient
The {@link oajr.mock2.MockRestClient} class can also be used for testing of {@link oaj.http.remote.Remote}-annotated interfaces against {@link oajr.annotation.Rest @Rest}-annotated resources.