Skip to main content
 

Is there an easy way to null-mock an RPC interface in Java/Apps-Framework?  I couldn't figure it out with some time in code search

I'm seeing people using easymock and mockito to do this, but that feels wrong (RPCs don't return null!) and also means that these testing frameworks might find their way into production code if we're not careful.

Here's an example:

 import com.google.ccc.groups.index.server.api.proto2api.CommonGroupsApi.CommonGroupsIndexSyncService;
 ...
 @Mock
 CommonGroupsIndexSyncService.ClientInterface mockSyncServer;
...
    bind(CommonGroupsIndexSyncService.ClientInterface.class)
        .toInstance(mockSyncServer);

Perhaps we could have a stub implementation that could do the following:

1) Implement returning defaultValue for all requests.
2) Implement returning RpcException for all requests.

Extra credit for the following wrapper class that would allow for injecting behavior into the pre-rpc/post-rpc phases:

  public class ForwardingRpcClientInterface {
     ...
     pubilc T delegate() {}

    beforeRpc(...);
    afterRpc(...);

    public Object invoke(...) throws Throwable {
      // defaults to forwarding to delegate
    }
  }

With something like this you could implement a client-side cache or short-circuit requests here.  You could also implement the defaultValue/RpcException classes based on this.