gpt4 book ai didi

java - 如何使用 Mockito 测试和模拟用 Java 编写的 GRPC 服务

转载 作者:行者123 更新时间:2023-11-29 08:28:59 25 4
gpt4 key购买 nike

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;
}

我需要结合使用 Mockito 和 JUnit 测试。

最佳答案

测试服务的鼓励方法是使用进程内传输和普通 stub 。然后您可以像往常一样与服务通信,而无需大量模拟。过度使用的模拟会产生脆弱的测试,这些测试不会对被测试的代码灌输信心。

GrpcServerRule 在后台使用进程内传输。我们现在建议看一下示例的测试,从 hello world 开始。 .

编辑:我们现在推荐 GrpcCleanupRule 而不是 GrpcServerRule。您仍然可以引用 hello world 示例。

关于java - 如何使用 Mockito 测试和模拟用 Java 编写的 GRPC 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49871975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com