Skip to content

Commit a04ad90

Browse files
authored
examples: clarify about potential mistake in unit test
Resolves grpc#6161
1 parent ba0fd84 commit a04ad90

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

examples/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,11 @@ $ bazel-bin/hello-world-client
164164

165165
Examples for unit testing gRPC clients and servers are located in [examples/src/test](src/test).
166166

167-
In general, we DO NOT allow overriding the client stub.
168-
We encourage users to leverage `InProcessTransport` as demonstrated in the examples to
169-
write unit tests. `InProcessTransport` is light-weight and runs the server
167+
In general, we DO NOT allow overriding the client stub and we DO NOT support mocking final methods
168+
in gRPC-Java library. Users should be cautious that using tools like PowerMock or
169+
[mockito-inline](https://search.maven.org/search?q=g:org.mockito%20a:mockito-inline) can easily
170+
break this rule of thumb. We encourage users to leverage `InProcessTransport` as demonstrated in the
171+
examples to write unit tests. `InProcessTransport` is light-weight and runs the server
170172
and client in the same process without any socket/TCP connection.
171173

172174
Mocking the client stub provides a false sense of security when writing tests. Mocking stubs and responses

examples/example-kotlin/src/test/kotlin/io/grpc/examples/helloworld/HelloWorldClientTest.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,16 @@ class HelloWorldClientTest {
5252
@get:Rule
5353
val grpcCleanup = GrpcCleanupRule()
5454

55-
private val serviceImpl = mock(GreeterGrpc.GreeterImplBase::class.java, delegatesTo<Any>(object : GreeterGrpc.GreeterImplBase() {
55+
private val serviceImpl = mock(GreeterGrpc.GreeterImplBase::class.java, delegatesTo<Any>(
56+
object : GreeterGrpc.GreeterImplBase() {
57+
// By default the client will receive Status.UNIMPLEMENTED for all RPCs.
58+
// You might need to implement necessary behaviors for your test here, like this:
59+
//
60+
// override fun sayHello(req: HelloRequest, respObserver: StreamObserver<HelloReply>) {
61+
// respObserver.onNext(HelloReply.getDefaultInstance())
62+
// respObserver.onCompleted()
63+
}))
5664

57-
}))
5865
private var client: HelloWorldClient? = null
5966

6067
@Before

examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ public class HelloWorldClientTest {
5656
public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
5757

5858
private final GreeterGrpc.GreeterImplBase serviceImpl =
59-
mock(GreeterGrpc.GreeterImplBase.class, delegatesTo(new GreeterGrpc.GreeterImplBase() {}));
59+
mock(GreeterGrpc.GreeterImplBase.class, delegatesTo(
60+
new GreeterGrpc.GreeterImplBase() {
61+
// By default the client will receive Status.UNIMPLEMENTED for all RPCs.
62+
// You might need to implement necessary behaviors for your test here, like this:
63+
//
64+
// @Override
65+
// public void sayHello(HelloRequest request, StreamObserver<HelloReply> respObserver) {
66+
// respObserver.onNext(HelloReply.getDefaultInstance());
67+
// respObserver.onCompleted();
68+
// }
69+
}));
70+
6071
private HelloWorldClient client;
6172

6273
@Before

0 commit comments

Comments
 (0)