GrpcExceptionHandler doesn't catch an exception in SpringBootTest
See original GitHub issueI’m trying to test the GrpcAdvice and GrpcExceptionHandler with the SpringBootTest. I have created the GrpcCoreExceptionHandler
@GrpcAdvice
public class GrpcCoreExceptionHandler
{
@GrpcExceptionHandler(IllegalArgumentException.class)
public StatusException handleResourceNotFoundException(IllegalArgumentException e) {
Status status = Status.INVALID_ARGUMENT.withDescription(e.getMessage()).withCause(e);
Metadata metadata = new Metadata();
return status.asException(metadata);
}
}
and have an integration test which invokes one of the methods in our gRPC PersonService
Here is the SpringBootTest setup:
@SpringBootTest(properties =
{
"grpc.server.inProcessName=PersonServiceIntegrationTest", "grpc.server.port=-1"
})
@RunWith(SpringRunner.class)
@Transactional("spTransactionManager")
public class PersonServiceIntegrationTest
{
@Autowired
private PersonService subject;
@SpyBean
private PersonDao personDao;
@Test
public void getPerson_whenExceptionBeenThrown_returnsErrorResponse()
{
doThrow(IllegalArgumentException.class).when(personDao).getPerson(any(), any());
StreamRecorder<PersonProto> responseObserver = StreamRecorder.create();
UserPersonIdProto userPersonId = UserPersonIdProto.newBuilder().build();
subject.getPerson(userPersonId, responseObserver);
Throwable error = responseObserver.getError();
assertNotNull(error);
}
}
In current setup the ExceptionHandler is not invoking and Exception is propagating farther to a spring RunAfterTestExecutionCallbacks layer.
But if i start the application locally and make a call with the Postman or any other gRPC client i can see that exception will be handled with the GrpcExceptionHandler. So, it looks like there is some conflict with between the SpringBootTest configuration and GrpcAdvice initilization.
Issue Analytics
- State:
- Created a year ago
- Comments:9
Top Results From Across the Web
Springboot exception handler doesn't catch exception
Your class will only catch errors raised from within a controller. The error you are seeing occurs before a controller is invoked, as...
Read more >Testing Exceptions with Spring MockMvc - Baeldung
In this short article, we'll see how exceptions should be thrown in our controllers and how to test these exceptions using Spring MockMvc....
Read more >Complete Guide to Exception Handling in Spring Boot
This article showcases various ways to handle exceptions in a Spring Boot Application.
Read more >MockMvc doesn't use spring-boot's mvc exception handler
I'm trying to test my Controller using MockMvc. The service used by Controller throws RuntimeException if something is wrong.
Read more >Exception Handling in Spring MVC
Exceptions thrown outside the Spring MVC framework, such as from a servlet Filter, are still reported by the Spring Boot fallback error page....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you for your help. The real client injection works fine.
Yes: use @GrpcClient on a stub and then call your method there.
https://github.com/yidongnan/grpc-spring-boot-starter/blob/0194be1c68faaeea251ff2edb063b37121988554/tests/src/test/java/net/devh/boot/grpc/test/setup/AbstractSimpleServerClientTest.java#L88