简体   繁体   English

根据Mockito的参数,从void方法抛出异常

[英]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. 不幸的是,我无法使用PowerMock。

Are you using some DI framework for your application? 您是否在为应用程序使用一些DI框架? 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. 如果是,那么您始终可以扩展所述类(假设它不是最​​终类),并使用您自己的实现重写所述emailException方法以进行集成测试。 Of course, you should Inject this extended class into your application. 当然,您应该将此扩展类注入您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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