【发布时间】:2018-08-14 14:36:46
【问题描述】:
我目前有一个模拟,它具有针对特定输入集的特定行为。 其他所有输入都应返回特定响应。
例如:
Mockito.when(classInstance.myFunc(Mockito.eq("Option1"))).thenReturn(answer1);
Mockito.when(classInstance.myFunc(Mockito.eq("Option2"))).thenReturn(answer2);
Mockito.when(classInstance.myFunc(Mockito.eq("Option3"))).thenReturn(answer3);
Mockito.when(classInstance.myFunc(Mockito.eq("Option4"))).thenReturn(answer4);
// Return defaultAnswer if and(and(not("Option1"), not("Option2")), and(not("Option3"), not("Option4")))
Mockito.when(classInstance.myFunc(AdditionalMatchers.and(AdditionalMatchers.and(AdditionalMatchers.not(Mockito.eq("Option1")), AdditionalMatchers.not(Mockito.eq("Option2")), AdditionalMatchers.and(AdditionalMatchers.not(Mockito.eq("Option3")), AdditionalMatchers.not(Mockito.eq("Option4")))).thenReturn(defaultAnswer);
我最大的麻烦是and(and(not("Option1"), not("Option2")), and(not("Option3"), not("Option4"))) 行的复杂性。
我真的希望有一种更简单的方法来指定“其他所有内容”或只是“不在列表中:[“option1”,...]”的条件
是否有“组内”或类似内容的匹配器?
【问题讨论】:
-
如果在其他
eq()模拟之后只使用anyString()会发生什么?
标签: java unit-testing junit mockito