简体   繁体   English

Mockito JUnit对引发异常的方法进行测试

[英]Mockito JUnit test on method which throws exception

I have a method inside my service which throws exception for one method call. 我的服务中有一个方法,它为一个方法调用引发异常。 For example, I have a code like: 例如,我有一个类似的代码:

void myServiceMethod() {
    method1(); // passes

    method2(); // passes

    method3(); // throws exception

    method4(); // passes

    method5(); // passes
}

What I want, is to handle this method3 (in real program it is a static method), in some way, so that a program can continue executing further code such as method4 and method5 in this example. 我想要的是以某种方式处理此method3(在实际程序中为静态方法),以便程序可以继续执行本示例中的其他代码,例如method4和method5。
Is it possible in mockito junit test, to return any value instead of exception, or to just skip it? 在嘲笑junit测试中,是否可以返回任何值而不是异常,或者只是跳过它?

Your question is actually two: 您的问题实际上是两个:

1: how do I avoid execution the mocked method ? 1:如何避免执行模拟方法?
2: how do I mock static methods? 2:如何模拟static方法?

Here are the answeres: 答案如下:

  1. Mockito provides 2 APIs to configure your mock. Mockito提供2个API来配置您的模拟。 The more common (and more readable) for is: 较为常见(且更具可读性)的是:

     when( mock.mockedMethod() ) .thenReturn(someValue); 

    The problem her is that the real method is actually executed and just the result is replaced. 她的问题是实际方法实际执行了,只是结果被替换了。 Usually this is not a problem unless your method throws an (unchecked) exception based on the return values of other (mocked but possibly unconfigured) methods in the same object or tries to access methods on dependencies of the mocked object since they are null so that a NPE is thrown. 通常这不是问题,除非您的方法基于同一对象中其他(模拟但可能未配置)方法的返回值引发(未经检查的)异常,或者由于它们为null而尝试访问模拟对象的依赖项的方法,因此NPE被抛出。

    One way is to also configure return values for all the other methods in your mocked class. 一种方法是还为模拟类中的所有其他方法配置返回值。 But then you have to "open" your mocked classes API bye raising the visibility of all methods involved above private just for testing. 但是随后,您必须通过提高所有private方法所涉及的所有方法的可见性来“开放”您的模拟类API,仅用于测试。 But making such changes just for testing is bad design. 但是仅出于测试目的进行此类更改是不好的设计。

    To avoid that you need to use the other form which does not execute the configured method: 为了避免这种情况,您需要使用不执行配置的方法的其他形式:

     doReturn(someValue).when( mock ). mockedMethod(); 

    please mind that the closing brace moved from behind the method call to before the dot separating the mock variable from the method call... 请注意,右括号从方法调用的后面移到了将模拟变量与方法调用分开的点之前。

    This might also solve your problem with the static method in your dependency in your concrete example. 在您的具体示例中,这也可以解决依赖项中的static方法的问题。


  1. You stated that the method you want to mock is static in your production code. 您说过要模拟的方法在生产代码中是static

    The problem here is that you should not use static access in your code in the first place. 这里的问题是,您一开始就不应在代码中使用静态访问。 So the best way is to change your method to an instance method and provide is instance of the class providing your "serviceMethod". 因此,最好的方法是将您的方法更改为实例方法,并提供提供“ serviceMethod”的类的实例。 Then you can use plain Mockito to create a mock and replace it for testing easily. 然后,您可以使用普通的Mockito创建一个模拟并替换它以便于测试。

Some may argue you can use PowerMock to mock static and/or private methods. 有人可能会说您可以使用 PowerMock 模拟static和/或private方法。 While this is technically true I'd consider it a surrender to your bad design ... 虽然从技术上讲这是正确的,但我还是认为这是对您不良设计的屈服 ...

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

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