Skip to content

Commit d257633

Browse files
committed
added unit tests (closes #268 via b6f9a75 )
1 parent b6f9a75 commit d257633

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import io.grpc.Status
44

55
fun throwException(status: Status) {
66
throw status.asException()
7+
}
8+
fun throwException(ex: Exception) {
9+
throw ex
710
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ 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())){
53+
ExceptionUtilsKt.throwException(new NullPointerException());
54+
}
5255
ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION);
5356
}
5457

@@ -62,6 +65,10 @@ public void custom(Custom.CustomRequest request, StreamObserver<Custom.CustomRep
6265
public StreamObserver<Custom.CustomRequest> customStream(StreamObserver<Custom.CustomReply> responseObserver) {
6366
throw new StatusRuntimeException(Status.FAILED_PRECONDITION);
6467
}
68+
@GRpcExceptionHandler
69+
public Status handle(NullPointerException e, GRpcExceptionScope scope ){
70+
return Status.DATA_LOSS;
71+
}
6572

6673
}
6774

@@ -114,4 +121,12 @@ public void statusExceptionTest() {
114121
assertThat(statusRuntimeException.getStatus(), is(Status.FAILED_PRECONDITION));
115122
}
116123

124+
@Test
125+
public void npeExceptionTest() {
126+
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () ->
127+
CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom(Custom.CustomRequest.newBuilder().setName("NPE").build())
128+
);
129+
assertThat(statusRuntimeException.getStatus(), is(Status.DATA_LOSS));
130+
}
131+
117132
}

0 commit comments

Comments
 (0)