Shr*_*arg 4 java junit mockito grpc
protobuf 定义如下:
syntax = "proto3";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
Run Code Online (Sandbox Code Playgroud)
我需要将 Mockito 与 JUnit 测试一起使用。
测试服务的鼓励方法是使用进程内传输和普通存根。然后您可以像往常一样与服务进行通信,而无需进行大量模拟。过度使用的模拟会产生脆弱的测试,不会让人们对被测试的代码充满信心。
GrpcServerRule在幕后使用进程内传输。我们现在建议查看示例的测试,从hello world开始。
编辑:我们现在推荐GrpcCleanupRule超过GrpcServerRule. 您仍然可以参考 hello world 示例。
这个想法是存根响应和流观察者。
@Test
public void shouldTestGreeterService() throws Exception {
Greeter service = new Greeter();
HelloRequest req = HelloRequest.newBuilder()
.setName("hello")
.build();
StreamObserver<HelloRequest> observer = mock(StreamObserver.class);
service.sayHello(req, observer);
verify(observer, times(1)).onCompleted();
ArgumentCaptor<HelloReply> captor = ArgumentCaptor.forClass(HelloReply.class);
verify(observer, times(1)).onNext(captor.capture());
HelloReply response = captor.getValue();
assertThat(response.getStatus(), is(true));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8906 次 |
| 最近记录: |