mocking ServerCall

36 views
Skip to first unread message

Eugene Strulyov

unread,
Sep 25, 2017, 6:51:45 PM9/25/17
to grpc.io
Hi,

I'm trying to upgrade to 1.6.1 but I ran into this problem. Currently have the following code:

        requestHeaders = new Metadata();

        attributes = Attributes.newBuilder()

            .set(Grpc.TRANSPORT_ATTR_SSL_SESSION, sslSession)

            .build();

        when(call.getAttributes()).thenReturn(attributes);

        when(call.getMethodDescriptor()).thenReturn(methodDescriptor);

        when(sslSession.getPeerPrincipal()).thenReturn(peerPrincipal);

        validator = new CertAuthInterceptor(true, AUTH_MAP);

...


        when(peerPrincipal.getName()).thenReturn("my peer principal");

        when(methodDescriptor.getFullMethodName()).thenReturn("my method...");

        validator.interceptCall(call, requestHeaders, next);


This doesn't work any more because MethodDescriptor has been marked final and ServerCall has @DoNotMock annotation. How do I do this now? The annotation suggests using InProcessTransport, but it seems that Attributes are hard-coded in it, so I cannot provide sslSession. I also don't understand why I now have to jump through hoops to do such a simple test.


(CertAuthInterceptor is a ServerInterceptor that allows/denies a method call depending on the client certificate and AUTH_MAP).


thanks,


Eugene



Eugene Strulyov

unread,
Sep 25, 2017, 7:49:45 PM9/25/17
to grpc.io
never mind.

It looks like the only real culprit is MethodDescriptor class which has now been marked final. But mock can be replaced with a builder:

        MethodDescriptor<Object, Object> methodDescriptor = MethodDescriptor.newBuilder()

            .setFullMethodName(fullMethodName)

            .setType(MethodType.UNARY)

            .setRequestMarshaller(marshaller)

            .setResponseMarshaller(marshaller)

            .build();


The @DoNotMock annotation on ServiceCall does not seem to do anything.

Eugene

Carl Mastrangelo

unread,
Sep 27, 2017, 8:14:40 PM9/27/17
to grpc.io
The @DoNotMock is enforced by a compiler plugin if you ave it turned on.  Otherwise, it just acts as documentation.   In general we prefer that you use a dummy service with InProcessChannelBuilder, rather than mocking.   This makes for more reliable tests.
Reply all
Reply to author
Forward
0 new messages