Skip to content

Commit

Permalink
added unit tests (closes #268 via b6f9a75 )
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmlet committed Aug 14, 2023
1 parent b6f9a75 commit d257633
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import io.grpc.Status

fun throwException(status: Status) {
throw status.asException()
}
fun throwException(ex: Exception) {
throw ex
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ static class CustomService extends CustomServiceGrpc.CustomServiceImplBase {

@Override
public void anotherCustom(Custom.CustomRequest request, StreamObserver<Custom.CustomReply> responseObserver) {
if("NPE".equals(request.getName())){
ExceptionUtilsKt.throwException(new NullPointerException());
}
ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION);
}

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

}

Expand Down Expand Up @@ -114,4 +121,12 @@ public void statusExceptionTest() {
assertThat(statusRuntimeException.getStatus(), is(Status.FAILED_PRECONDITION));
}

@Test
public void npeExceptionTest() {
final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () ->
CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom(Custom.CustomRequest.newBuilder().setName("NPE").build())
);
assertThat(statusRuntimeException.getStatus(), is(Status.DATA_LOSS));
}

}

0 comments on commit d257633

Please sign in to comment.