从 grpc 测试调用外部存根 class
Call external stub from grpc test class
我正在尝试使用存根调用 grpc 服务(原型存在于不同的 jar 文件中)。但是,当我尝试调用该服务时,我得到 原因是 io.grpc.statusruntimeexception 未找到未实现的方法 。同样在主 class 中工作正常,但在测试用例中却没有。
DeviceGroupServiceImplBase deviceService = Mockito.mock(DeviceGroupServiceImplBase.class,
AdditionalAnswers.delegatesTo(新的 DeviceGroupServiceImplBase() {
}));
public void createInProcessServerAndChannel() throws IOException {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
// Create a server, add service, start, and register for automatic graceful
// shutdown.
grpcCleanup.register(
InProcessServerBuilder.forName(serverName).directExecutor().addService(deviceService).build().start());
// Create a client channel and register for automatic graceful shutdown.
ManagedChannel channel = grpcCleanup
.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
// Create a DeviceGroupServiceClient using the in-process channel;
groupStub = DeviceGroupServiceGrpc.newBlockingStub(channel);
}
// Test case code
When("user calls getDevice with valid deviceUUID {string}", (String deviceUUID) -> {
DeviceUuid request = DeviceUuid.newBuilder().setDeviceUuid(deviceUUID).build();
DeviceGroup groupData = groupStub.getDeviceGroupByDeviceUuid(request);
});
您需要实现 getDeviceGroupByDeviceUuid
,默认情况下它处于 returns 未实现状态。您可以验证它是否正在调用 ServerCall#asyncUnimplementedUnaryCall
.
DeviceGroupServiceImplBase deviceService =
Mockito.mock(
DeviceGroupServiceImplBase.class,
AdditionalAnswers.delegatesTo(
new DeviceGroupServiceImplBase() {
@Override
public void getDeviceGroupByDeviceUuid(
DeviceUuid request, StreamObserver<DeviceGroup> responseObserver) {
// TODO: implement
}
}));
我正在尝试使用存根调用 grpc 服务(原型存在于不同的 jar 文件中)。但是,当我尝试调用该服务时,我得到 原因是 io.grpc.statusruntimeexception 未找到未实现的方法 。同样在主 class 中工作正常,但在测试用例中却没有。
DeviceGroupServiceImplBase deviceService = Mockito.mock(DeviceGroupServiceImplBase.class, AdditionalAnswers.delegatesTo(新的 DeviceGroupServiceImplBase() { }));
public void createInProcessServerAndChannel() throws IOException {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
// Create a server, add service, start, and register for automatic graceful
// shutdown.
grpcCleanup.register(
InProcessServerBuilder.forName(serverName).directExecutor().addService(deviceService).build().start());
// Create a client channel and register for automatic graceful shutdown.
ManagedChannel channel = grpcCleanup
.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
// Create a DeviceGroupServiceClient using the in-process channel;
groupStub = DeviceGroupServiceGrpc.newBlockingStub(channel);
}
// Test case code
When("user calls getDevice with valid deviceUUID {string}", (String deviceUUID) -> {
DeviceUuid request = DeviceUuid.newBuilder().setDeviceUuid(deviceUUID).build();
DeviceGroup groupData = groupStub.getDeviceGroupByDeviceUuid(request);
});
您需要实现 getDeviceGroupByDeviceUuid
,默认情况下它处于 returns 未实现状态。您可以验证它是否正在调用 ServerCall#asyncUnimplementedUnaryCall
.
DeviceGroupServiceImplBase deviceService =
Mockito.mock(
DeviceGroupServiceImplBase.class,
AdditionalAnswers.delegatesTo(
new DeviceGroupServiceImplBase() {
@Override
public void getDeviceGroupByDeviceUuid(
DeviceUuid request, StreamObserver<DeviceGroup> responseObserver) {
// TODO: implement
}
}));