本文介紹了如何編寫不等于某物的匹配器的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在嘗試為通話創(chuàng)建模擬.假設(shè)我有這個(gè)方法,我正在嘗試存根:
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";
}
}
我想模擬的是:第一個(gè)實(shí)例是
What I am trying to mock out is: 1st instance is
when(classA.getString(eq("a")).thenReturn(...);`
在同一個(gè)測(cè)試用例中
when(classA.getString([anything that is not a])).thenReturn(somethingelse);
第二種情況是我的問(wèn)題:我如何匹配 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");
希望對(duì)你有幫助.
這篇關(guān)于如何編寫不等于某物的匹配器的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!