question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GrpcExceptionHandler doesn't catch an exception in SpringBootTest

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
pc-avasilevcommented, May 18, 2022

Thank you for your help. The real client injection works fine.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found