MockRemoteResource
The {@link oajr.mock2.MockRemoteResource} class is used for serverless unit testing of {@link oaj.http.remote.RemoteResource @RemoteResource}-annotated
classes.
The {@link oajr.mock2.MockRemoteResource} API requires a {@link oajr.annotation.RestResource @RestResource}-annotated class to be used as
an underlying mocked resource to process the request and return a response.
// 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() {
MyRemoteInterface mri = MockRemoteResource.build(MyRemoteInterface.class, MyRest.class);
assertEquals(123, mri.echoQuery(123));
}
It looks simple, but there's a lot going on here.
Remote resource interfaces are normally created through the {@link oajrc.RestClient#getRemoteResource(Class)} method.
The {@link oajr.mock2.MockRemoteResource} will create a {@link oajrc.RestClient} using a specialized HttpClientConnectionManager
designed to transform client-side HttpRequest/HttpResponse objects into server-side
{@link oajr.mock2.MockServletRequest}/{@link oajr.mock2.MockServletResponse} objects and then pass those to the {@link oajr.mock2.MockRest}
object described in the previous section.
All aspects of the client and server side code are tested, yet no servlet container is required. The actual
over-the-wire serialization is the only aspect being bypassed.
By default, the {@link oajr.mock2.MockRemoteResource} class uses JSON marshalling.
This can be overridden via any of the following methods:
- {@link oajr.mock2.MockRemoteResource#build(Class,Object,Marshall)}
- {@link oajr.mock2.MockRemoteResource#build(Class,Object,Serializer,Parser)}
- {@link oajr.mock2.MockRemoteResource#create(Class,Object,Marshall)}
- {@link oajr.mock2.MockRemoteResource#create(Class,Object,Serializer,Parser)}
The {@link oajr.mock2.MockRemoteResource} class has a debug mode that will cause your HTTP requests and responses to
be sent to the console on both the client and server sides:
@Test
public void testProxy() {
MyRemoteInterface mri = MockRemoteResource
.create(MyRemoteInterface.class, MyRest.class)
.debug()
.build();
assertEquals(123, mri.echoQuery(123));
}