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);

添加静态导入要好得多:)