Mockito: How to match any enum parameter

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

I have this method declared like this

private Long doThings(MyEnum enum, Long otherParam); and this enum

public enum MyEnum{   VAL_A,   VAL_B,   VAL_C } 

Question: How do I mock doThings() calls? I cannot match any MyEnum.

The following doesn't work:

Mockito.when(object.doThings(Matchers.any(), Matchers.anyLong()))         .thenReturn(123L); 

回答1:

Matchers.any(Class) will do the trick:

Mockito.when(object.doThings(Matchers.any(MyEnum.class), Matchers.anyLong()))     .thenReturn(123L); 

As a side note: consider using Mockito static imports:

import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; 

Mocking gets a lot shorter:

when(object.doThings(any(MyEnum.class), anyLong())).thenReturn(123L); 


回答2:

Apart from the above solution try this...

when(object.doThings((MyEnum)anyObject(), anyLong()).thenReturn(123L); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!