gRPC单元测试的最佳实践

时间:2019-08-14 22:51:34

标签: java junit5 grpc-java

这是对gRPC服务执行集成测试时的最佳实践吗?即在每次单元测试后使用@BeforeEach和@AfterEach注释打开和关闭通道

private ManagedChannel channel;

private DepartmentServiceGrpc.DepartmentServiceBlockingStub deptService;

@BeforeEach
public void initEach(){
    channel = ManagedChannelBuilder.forAddress("localhost", 8080)
            .usePlaintext()
            .build();

    deptService = DeptServiceGrpc.newBlockingStub(channel);
}

@AfterEach
public void cleanUpEach(){
    channel.shutdown();
}

2 个答案:

答案 0 :(得分:0)

gRPC提供了名为GrpcCleanupRule的junit4规则。请注意,这是junit4规则。

答案 1 :(得分:0)

如果使用的是JUnit 4,则可以使用GrpcCleanupRule。如果您使用的是JUnit 5,则可以使用我创建的OSS grpc-test