模拟在Java中多次调用的方法。

huangapple go评论79阅读模式
英文:

Mocking a mothod that is being called multiple times in java

问题

我想测试一种方法,即多次调用另一种方法。

Class Sample{
OtherClass otherClass;
public OutputPoJo callABCMultipleTImes(){
OutputPoJo outputPojo;
try{
outputPojo = otherClass.callABC();
} catch(RuntimeException ex){
//Retrying call one more time
outputPojo = otherClass.callABC();
}
return outputPojo;
}
}

我想测试这个方法。为此,我正在做类似这样的事情,并且对于不同的组合,这对我效果很好。

public void testCallABCMultipleTImes(){
when(otherClass.callABC())
.thenThrow(new RuntimeException("第一次尝试失败。")).
.thenReturn(new OutputPOJO());
mockedSampleClass.callABCMultipleTImes();
Mockito.verify(otherClass, Mockito.times(2)).callABC();
}

基本上,我正在检查第一次是否得到了异常,第二次是否得到了成功的响应。我通过验证该方法被调用了两次来进行检查。

这是测试这种情况的正确方法吗,还是还有其他方法?

谢谢!

<details>
<summary>英文:</summary>

I want to test one method which is calling another method multiple times. 

Class Sample{
OtherClass otherClass;
public OutputPoJo callABCMultipleTImes(){
OutputPoJo outputPojo;
try{
outputPojo = otherClass.callABC();
} catch(RuntimeException ex){
//Retrying call one more time
outputPojo = otherClass.callABC();
}
return outputPojo;
}
}

I want to test this method. For that, I am doing something like this and that is working fine for me for different combination.

public void testCallABCMultipleTImes(){
when(otherClass.callABC())
.thenThrow(new RuntimeException("First try failed.")).
.thenReturn(new OutputPOJO());
mockedSampleClass.callABCMultipleTImes();
Mockito.verify(otherClass,Mockito.times(2)).callABC();
}


Basically, I am checking that I got exception first time and second time, I got successful response. I am checking this by verifying that method is being called twice.

Is that the correct way to test this kind of scenario or is there any other way?

Thanks!

</details>


# 答案1
**得分**: 1

你应该测试你的方法是否具有正确的行为,即它是否返回你期望的值:

```java
public void testCallABCMultipleTImes(){
   OutputPOJO value  = new OutputPOJO();
   when(otherClass.callABC())
     .thenThrow(new RuntimeException("第一次尝试失败。"))
     .thenReturn(value);
   assertEquals(value, mockedSampleClass.callABCMultipleTImes());
}
```

理论上来说,没有必要调用`verify`,因为你的方法返回正确的值就证明它做对了。

<details>
<summary>英文:</summary>

You should test that your method has the right behaviour, i.e. that it returns the value you expect it to return:

    public void testCallABCMultipleTImes(){
       OutputPOJO value  = new OutputPOJO();
       when(otherClass.callABC())
         .thenThrow(new RuntimeException(&quot;First try failed.&quot;))
         .thenReturn(value);
       assertEquals(value, mockedSampleClass.callABCMultipleTImes());
    }

In theory there&#39;s no need to call `verify`, because the fact that your method returned the correct value proves that it did the right thing.

</details>



huangapple
  • 本文由 发表于 2020年4月9日 20:47:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61121514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定