[英]How to use Mockito to verify a method is called in another method?
[英]How to verify a method was called inside another method with Mockito
我對 Mockito 相當陌生,我一直在尋找一種方法來驗證如果我使用正確的字符串調用 filter() 方法,則 foo 方法將被調用一次。
public class A
{
private final Config _config;
public A(Config config) { _config = config; }
public void filter(String str)
{
if(str.startsWith("a"))
{
if(str.contains("z"))
{
foo(config.getName());
}
}
}
private void foo(String bar)
{
(...)
}
}
這是我當前的代碼:
@Test
public void testOne()
{
Config config = new Config(configFile);
A a = Mockito.spy(new A(config));
a.filter("abcz");
verify(a, times(1)).foo(someString);
}
在這樣的測試中嘗試更通用。 如果您不需要指定應傳遞的確切參數,則只需使用any()
:
import static org.mockito.ArgumentMatchers.any;
verify(a).foo(any(String.class));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.