{8.1.0-new, 8.1.2-updated} MockRemote

The {@link oajr.mock2.MockRemote} class is used for serverless unit testing of {@link oaj.http.remote.Remote @Remote}-annotated classes.

The {@link oajr.mock2.MockRemote} API requires a {@link oajr.annotation.Rest @Rest}-annotated class to be used as an underlying mocked resource to process the request and return a response.

##### Example:

// Our remote resource to test. @Remote public interface MyRemoteInterface { @RemoteMethod(httpMethod="GET", path="/echoQuery") public int echoQuery(@Query(name="id") int id); } // Our mocked-up REST interface to test against. @Rest public class MyRest { @RestMethod(name=GET, path="/echoQuery") public int echoQuery(@Query("id") String id) { return id; } } @Test public void testProxy() { MyRemoteInterface mri = MockRemote.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#getRemote(Class)} method. The {@link oajr.mock2.MockRemote} 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.MockRemote} class uses JSON marshalling. This can be overridden via any of the following methods:

- {@link oajr.mock2.MockRemote#build(Class,Object,Marshall)}
- {@link oajr.mock2.MockRemote#build(Class,Object,Serializer,Parser)}
- {@link oajr.mock2.MockRemote#create(Class,Object,Marshall)}
- {@link oajr.mock2.MockRemote#create(Class,Object,Serializer,Parser)}

The {@link oajr.mock2.MockRemote} 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 = MockRemote .create(MyRemoteInterface.class, MyRest.class) .debug() .build(); assertEquals(123, mri.echoQuery(123)); }