cscan
cscan

Reputation: 3840

Throw exception from void method depending on parameters with Mockito

Our application includes an error reporting microservice. When an exception is thrown in our application that information is saved in the database, a message is sent to the error reporting microservice, and the devs are sent an email. This is great in production but makes integration tests a lot more obtuse as the exception is never printed and the test doesn't necessarily fail. I'd like to mock the error reporting microservice such that it looks up the message and stack in the database, logs it, and then throws an exception. Is that something which is possible?

Here's the signature of the method which should be mocked:

public void emailException(Long exceptionInfoPrimaryKey);

Unfortunately I cannot use PowerMock.

Upvotes: 0

Views: 315

Answers (1)

anand1st
anand1st

Reputation: 1176

Are you using some DI framework for your application? If yes, then you can always extend the said class (assuming it's not final) and override the said emailException method with your own implementation for integration testing. Of course, you should Inject this extended class into your application.

Upvotes: 1

Related Questions