48

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

2 Answers 2

76

Matchers.any(Class) will do the trick:

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

null will be excluded with Matchers.any(Class). If you want to include null you must use the more generic Matchers.any().

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);
1
  • Adding static imports is far better one :) Nov 13, 2013 at 9:28
1

Apart from the above solution try this...

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.