File tree Expand file tree Collapse file tree 3 files changed +26
-6
lines changed
example-kotlin/src/test/kotlin/io/grpc/examples/helloworld
src/test/java/io/grpc/examples/helloworld Expand file tree Collapse file tree 3 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -164,9 +164,11 @@ $ bazel-bin/hello-world-client
164164
165165Examples 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
170172and client in the same process without any socket/TCP connection.
171173
172174Mocking the client stub provides a false sense of security when writing tests. Mocking stubs and responses
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments