examples: clarify about potential mistake in unit test

Resolves #6161
This commit is contained in:
ZHANG Dapeng 2019-09-18 13:24:56 -07:00 committed by GitHub
parent ba0fd84d79
commit a04ad90888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 6 deletions

View File

@ -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

View File

@ -52,9 +52,16 @@ class HelloWorldClientTest {
@get:Rule
val grpcCleanup = GrpcCleanupRule()
private val serviceImpl = mock(GreeterGrpc.GreeterImplBase::class.java, delegatesTo<Any>(object : GreeterGrpc.GreeterImplBase() {
private val serviceImpl = mock(GreeterGrpc.GreeterImplBase::class.java, delegatesTo<Any>(
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<HelloReply>) {
// respObserver.onNext(HelloReply.getDefaultInstance())
// respObserver.onCompleted()
}))
}))
private var client: HelloWorldClient? = null
@Before

View File

@ -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<HelloReply> respObserver) {
// respObserver.onNext(HelloReply.getDefaultInstance());
// respObserver.onCompleted();
// }
}));
private HelloWorldClient client;
@Before