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
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
164
164
165
165
Examples for unit testing gRPC clients and servers are located in [ examples/src/test] ( src/test ) .
166
166
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
170
172
and client in the same process without any socket/TCP connection.
171
173
172
174
Mocking 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 {
52
52
@get:Rule
53
53
val grpcCleanup = GrpcCleanupRule ()
54
54
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
+ }))
56
64
57
- }))
58
65
private var client: HelloWorldClient ? = null
59
66
60
67
@Before
Original file line number Diff line number Diff line change @@ -56,7 +56,18 @@ public class HelloWorldClientTest {
56
56
public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule ();
57
57
58
58
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
+
60
71
private HelloWorldClient client ;
61
72
62
73
@ Before
You can’t perform that action at this time.
0 commit comments