Skip to content

Commit 11d1660

Browse files
committed
more tests (ref #373)
1 parent 13bbfbc commit 11d1660

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/recovery/GRpcStatusRuntimeExceptionTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,17 @@ static class CustomService extends CustomServiceGrpc.CustomServiceImplBase {
4949

5050
@Override
5151
public void anotherCustom(Custom.CustomRequest request, StreamObserver<Custom.CustomReply> responseObserver) {
52-
if("NPE".equals(request.getName())){
52+
if ("NPE".equals(request.getName())) {
5353
ExceptionUtilsKt.throwException(new NullPointerException());
5454
}
55-
ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION);
55+
if ("RT_ERROR".equals(request.getName())) {
56+
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
57+
}
58+
if ("ERROR".equals(request.getName())) {
59+
responseObserver.onError(Status.OUT_OF_RANGE.asException());
60+
} else {
61+
ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION);
62+
}
5663
}
5764

5865
@Override
@@ -65,8 +72,9 @@ public void custom(Custom.CustomRequest request, StreamObserver<Custom.CustomRep
6572
public StreamObserver<Custom.CustomRequest> customStream(StreamObserver<Custom.CustomReply> responseObserver) {
6673
throw new StatusRuntimeException(Status.FAILED_PRECONDITION);
6774
}
75+
6876
@GRpcExceptionHandler
69-
public Status handle(NullPointerException e, GRpcExceptionScope scope ){
77+
public Status handle(NullPointerException e, GRpcExceptionScope scope) {
7078
return Status.DATA_LOSS;
7179
}
7280

@@ -129,4 +137,22 @@ public void npeExceptionTest() {
129137
assertThat(statusRuntimeException.getStatus(), is(Status.DATA_LOSS));
130138
}
131139

140+
@Test
141+
public void errorExceptionTest() {
142+
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () ->
143+
CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom(
144+
Custom.CustomRequest.newBuilder().setName("ERROR").build()
145+
)
146+
);
147+
assertThat(statusRuntimeException.getStatus(), is(Status.OUT_OF_RANGE));
148+
}
149+
public void rtErrorExceptionTest() {
150+
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () ->
151+
CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom(
152+
Custom.CustomRequest.newBuilder().setName("RT_ERROR").build()
153+
)
154+
);
155+
assertThat(statusRuntimeException.getStatus(), is(Status.NOT_FOUND));
156+
}
157+
132158
}

0 commit comments

Comments
 (0)