問(wèn)題描述
我正在嘗試驗(yàn)證是否在 DAO 內(nèi)部調(diào)用了一個(gè) (void) 方法 - 我正在使用一個(gè)提交點(diǎn),該提交點(diǎn)發(fā)送到該點(diǎn)的結(jié)果列表,重置列表并繼續(xù).假設(shè)我在列表中有 4 件事并且我的提交點(diǎn)為 1,我希望發(fā)送"方法被調(diào)用 4 次.我可以通過(guò)編寫(xiě)驗(yàn)證該方法是否被調(diào)用一次
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()
它通過(guò)了.. 但我想驗(yàn)證它被調(diào)用的次數(shù).我會(huì)認(rèn)為
it passes.. but I want to verify the number of times it was called. I would think that
Mockito.verify(mock.send(), times(4))
就足夠了,但它說(shuō)參數(shù)不正確,無(wú)法驗(yàn)證.
would be sufficient, but it says the parameters are not correct for verify.
順便說(shuō)一句,如果我將 Mockito.verify(mock).send()
更改為 Mockito.verify(mock.send())
或 Mockito.verify((mock).send())
我得到同樣的錯(cuò)誤.對(duì)此有何想法?
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
是您的模擬對(duì)象,mode
是描述應(yīng)該如何驗(yàn)證模擬的 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");
您將需要來(lái)自 的這些靜態(tài)導(dǎo)入Mockito
類,以便使用 verify
方法和這些驗(yàn)證模式:
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;
所以在你的情況下,正確的語(yǔ)法是:
So in your case the correct syntax will be:
Mockito.verify(mock, times(4)).send()
這將驗(yàn)證方法 send
在模擬對(duì)象上被調(diào)用 4 次.如果調(diào)用少于或多于 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.
如果你只是想檢查,如果方法被調(diào)用過(guò)一次,那么你不需要傳遞一個(gè)VerificationMode
.一個(gè)簡(jiǎn)單的
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");
就夠了.它在內(nèi)部使用 verify(mock, ti??mes(1)).someMethod("was called once");
.
would be enough. It internally uses verify(mock, times(1)).someMethod("was called once");
.
可以在同一個(gè) mock 上進(jìn)行多個(gè)驗(yàn)證調(diào)用以實(shí)現(xiàn)介于"驗(yàn)證.Mockito 不支持這樣的 verify(mock, between(4,6)).someMethod("was called between 4 and 6 times");
,但我們可以寫(xiě)
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");
相反,獲得相同的行為.邊界被包含,因此當(dāng)方法被調(diào)用 4、5 或 6 次時(shí),測(cè)試用例為綠色.
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.
這篇關(guān)于Java 使用 Mockito 驗(yàn)證 void 方法調(diào)用 n 次的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!