简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM