我正在尝试为呼叫创建模拟。说我有尝试这种方法,请尝试:
class ClassA {
public String getString(String a) {
return a + "hey";
}
}
我想模拟的是:第一个实例是
when(classA.getString(eq("a")).thenReturn(...);`
在同一测试用例中
when(classA.getString([anything that is not a])).thenReturn(somethingelse);
第二种情况是我的问题:anyString()
除“ a”外,我该如何匹配?
通过Mockito
框架,您可以使用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");
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句