Java Mockito:如何匹配任何枚举参数
我已经像这样声明了这个方法Java Mockito:如何匹配任何枚举参数,java,enums,mockito,Java,Enums,Mockito,我已经像这样声明了这个方法 private Long doThings(MyEnum enum,Long otherParam) 这个枚举 public enum MyEnum{ VAL_A, VAL_B, VAL_C } 问题:如何模拟doThings()调用? 我无法匹配任何MyEnum 以下操作不起作用: Mockito.when(object.doThings(Matchers.any(), Matchers.anyLong())) .thenReturn(
private Long doThings(MyEnum enum,Long otherParam)代码>
这个枚举
public enum MyEnum{
VAL_A,
VAL_B,
VAL_C
}
问题:如何模拟doThings()
调用?
我无法匹配任何MyEnum
以下操作不起作用:
Mockito.when(object.doThings(Matchers.any(), Matchers.anyLong()))
.thenReturn(123L);
匹配器。任何(类)
都可以:
Mockito.when(object.doThings(Matchers.any(MyEnum.class), Matchers.anyLong()))
.thenReturn(123L);
null
将被匹配器排除。任何(类)
。如果要包含null
,必须使用更通用的匹配器.any()
作为一个旁注:考虑使用<代码> Mockito <代码>静态导入:
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
嘲弄变得更短了:
when(object.doThings(any(MyEnum.class), anyLong())).thenReturn(123L);
除了上述解决方案,请尝试此
when(object.doThings((MyEnum)anyObject(), anyLong()).thenReturn(123L);
添加静态导入要好得多:)