本文介紹了如何驗(yàn)證沒有使用 Mockito 調(diào)用特定方法?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
如何驗(yàn)證方法是否沒有在對(duì)象的依賴項(xiàng)上調(diào)用?
How to verify that a method is not called on an object's dependency?
例如:
public interface Dependency {
void someMethod();
}
public class Foo {
public bar(final Dependency d) {
...
}
}
通過 Foo 測(cè)試:
public class FooTest {
@Test
public void dependencyIsNotCalled() {
final Foo foo = new Foo(...);
final Dependency dependency = mock(Dependency.class);
foo.bar(dependency);
**// verify here that someMethod was not called??**
}
}
推薦答案
更有意義:
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
// ...
verify(dependency, never()).someMethod();
這里有這個(gè)功能的文檔 §4 "驗(yàn)證調(diào)用的確切數(shù)量/至少 x/never",并且 never
javadoc 是 這里.
The documentation of this feature is there §4 "Verifying exact number of invocations / at least x / never", and the never
javadoc is here.
這篇關(guān)于如何驗(yàn)證沒有使用 Mockito 調(diào)用特定方法?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!