mirror of https://github.com/grpc/grpc-java.git
parent
ba0fd84d79
commit
a04ad90888
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue