本文介紹了如何編寫不等于某物的匹配器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試為通話創建模擬.假設我有這個方法,我正在嘗試存根:
I am trying to create a mock for a call. Say I have this method I am trying to stub out:
class ClassA {
public String getString(String a) {
return a + "hey";
}
}
我想模擬的是:第一個實例是
What I am trying to mock out is: 1st instance is
when(classA.getString(eq("a")).thenReturn(...);`
在同一個測試用例中
when(classA.getString([anything that is not a])).thenReturn(somethingelse);
第二種情況是我的問題:我如何匹配 anyString()
而不是 "a"?
The 2nd case is my question: How do I match anyString()
other than "a"?
推薦答案
有了 Mockito
框架,你可以使用 AdditionalMatchers
With Mockito
framework, you can use AdditionalMatchers
ClassA classA = Mockito.mock(ClassA.class);
Mockito.when(classA.getString(Matchers.eq("a"))).thenReturn("something");
Mockito.when(classA.getString(AdditionalMatchers.not(Matchers.eq("a")))).thenReturn("something else");
希望對你有幫助.
這篇關于如何編寫不等于某物的匹配器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!