問題描述
我正在嘗試驗證是否在 DAO 內部調用了一個 (void) 方法 - 我正在使用一個提交點,該提交點發送到該點的結果列表,重置列表并繼續.假設我在列表中有 4 件事并且我的提交點為 1,我希望發送"方法被調用 4 次.我可以通過編寫驗證該方法是否被調用一次
I'm trying to verify that a (void) method is being called inside of a DAO - I'm using a commit point that sends a list of results up to that point, resets the list and continues. Say I have 4 things in the list and I have a commit point of 1, I would expect the "send" method to be called 4 times. I can verify that the method gets called once by writing
Mockito.verify(mock).send()
它通過了.. 但我想驗證它被調用的次數.我會認為
it passes.. but I want to verify the number of times it was called. I would think that
Mockito.verify(mock.send(), times(4))
就足夠了,但它說參數不正確,無法驗證.
would be sufficient, but it says the parameters are not correct for verify.
順便說一句,如果我將 Mockito.verify(mock).send()
更改為 Mockito.verify(mock.send())
或 Mockito.verify((mock).send())
我得到同樣的錯誤.對此有何想法?
Incidentally, if I change Mockito.verify(mock).send()
to Mockito.verify(mock.send())
or Mockito.verify((mock).send())
I get the same error. Thoughts on this?
推薦答案
必要的方法是Mockito#verify:
public static <T> T verify(T mock,
VerificationMode mode)
mock
是您的模擬對象,mode
是描述應該如何驗證模擬的 VerificationMode
.可能的模式是:
mock
is your mocked object and mode
is the VerificationMode
that describes how the mock should be verified. Possible modes are:
verify(mock, times(5)).someMethod("was called five times");
verify(mock, never()).someMethod("was never called");
verify(mock, atLeastOnce()).someMethod("was called at least once");
verify(mock, atLeast(2)).someMethod("was called at least twice");
verify(mock, atMost(3)).someMethod("was called at most 3 times");
verify(mock, atLeast(0)).someMethod("was called any number of times"); // useful with captors
verify(mock, only()).someMethod("no other method has been called on the mock");
您將需要來自 的這些靜態導入Mockito
類,以便使用 verify
方法和這些驗證模式:
You'll need these static imports from the Mockito
class in order to use the verify
method and these verification modes:
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
所以在你的情況下,正確的語法是:
So in your case the correct syntax will be:
Mockito.verify(mock, times(4)).send()
這將驗證方法 send
在模擬對象上被調用 4 次.如果調用少于或多于 4 次,它將失敗.
This verifies that the method send
was called 4 times on the mocked object. It will fail if it was called less or more than 4 times.
如果你只是想檢查,如果方法被調用過一次,那么你不需要傳遞一個VerificationMode
.一個簡單的
If you just want to check, if the method has been called once, then you don't need to pass a VerificationMode
. A simple
verify(mock).someMethod("was called once");
就夠了.它在內部使用 verify(mock, ti??mes(1)).someMethod("was called once");
.
would be enough. It internally uses verify(mock, times(1)).someMethod("was called once");
.
可以在同一個 mock 上進行多個驗證調用以實現介于"驗證.Mockito 不支持這樣的 verify(mock, between(4,6)).someMethod("was called between 4 and 6 times");
,但我們可以寫
It is possible to have multiple verification calls on the same mock to achieve a "between" verification. Mockito doesn't support something like this verify(mock, between(4,6)).someMethod("was called between 4 and 6 times");
, but we can write
verify(mock, atLeast(4)).someMethod("was called at least four times ...");
verify(mock, atMost(6)).someMethod("... and not more than six times");
相反,獲得相同的行為.邊界被包含,因此當方法被調用 4、5 或 6 次時,測試用例為綠色.
instead, to get the same behaviour. The bounds are included, so the test case is green when the method was called 4, 5 or 6 times.
這篇關于Java 使用 Mockito 驗證 void 方法調用 n 次的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!