diff --git a/examples/README.md b/examples/README.md index d98fc0d72d..a5fa1e1b68 100644 --- a/examples/README.md +++ b/examples/README.md @@ -164,9 +164,11 @@ $ bazel-bin/hello-world-client Examples for unit testing gRPC clients and servers are located in [examples/src/test](src/test). -In general, we DO NOT allow overriding the client stub. -We encourage users to leverage `InProcessTransport` as demonstrated in the examples to -write unit tests. `InProcessTransport` is light-weight and runs the server +In general, we DO NOT allow overriding the client stub and we DO NOT support mocking final methods +in gRPC-Java library. Users should be cautious that using tools like PowerMock or +[mockito-inline](https://search.maven.org/search?q=g:org.mockito%20a:mockito-inline) can easily +break this rule of thumb. We encourage users to leverage `InProcessTransport` as demonstrated in the +examples to write unit tests. `InProcessTransport` is light-weight and runs the server and client in the same process without any socket/TCP connection. Mocking the client stub provides a false sense of security when writing tests. Mocking stubs and responses diff --git a/examples/example-kotlin/src/test/kotlin/io/grpc/examples/helloworld/HelloWorldClientTest.kt b/examples/example-kotlin/src/test/kotlin/io/grpc/examples/helloworld/HelloWorldClientTest.kt index a3f9ce54b7..ffd06feaab 100644 --- a/examples/example-kotlin/src/test/kotlin/io/grpc/examples/helloworld/HelloWorldClientTest.kt +++ b/examples/example-kotlin/src/test/kotlin/io/grpc/examples/helloworld/HelloWorldClientTest.kt @@ -52,9 +52,16 @@ class HelloWorldClientTest { @get:Rule val grpcCleanup = GrpcCleanupRule() - private val serviceImpl = mock(GreeterGrpc.GreeterImplBase::class.java, delegatesTo(object : GreeterGrpc.GreeterImplBase() { + private val serviceImpl = mock(GreeterGrpc.GreeterImplBase::class.java, delegatesTo( + object : GreeterGrpc.GreeterImplBase() { + // By default the client will receive Status.UNIMPLEMENTED for all RPCs. + // You might need to implement necessary behaviors for your test here, like this: + // + // override fun sayHello(req: HelloRequest, respObserver: StreamObserver) { + // respObserver.onNext(HelloReply.getDefaultInstance()) + // respObserver.onCompleted() + })) - })) private var client: HelloWorldClient? = null @Before diff --git a/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java b/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java index 4ba747dcd2..d0262d687e 100644 --- a/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java +++ b/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java @@ -56,7 +56,18 @@ public class HelloWorldClientTest { public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); private final GreeterGrpc.GreeterImplBase serviceImpl = - mock(GreeterGrpc.GreeterImplBase.class, delegatesTo(new GreeterGrpc.GreeterImplBase() {})); + mock(GreeterGrpc.GreeterImplBase.class, delegatesTo( + new GreeterGrpc.GreeterImplBase() { + // By default the client will receive Status.UNIMPLEMENTED for all RPCs. + // You might need to implement necessary behaviors for your test here, like this: + // + // @Override + // public void sayHello(HelloRequest request, StreamObserver respObserver) { + // respObserver.onNext(HelloReply.getDefaultInstance()); + // respObserver.onCompleted(); + // } + })); + private HelloWorldClient client; @Before