在看似正确的代码中获取org.mockito.exceptions.misusing.InvalidUseOfMatchersException

时间:2019-02-13 12:02:29

标签: mockito

我有这样的when通话:

    @Mock
    private MyEventFactory myEventFactory;

    @Mock 
    private MyEvent myEvent;
when(myEventFactory.createMyEvent(anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject())).thenReturn(myEvent);

但是我遇到例外

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
You cannot use argument matchers outside of verification or stubbing.
  

MyEventFactory是一个接口

这里可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您是否已初始化模拟?查看完整的代码会有所帮助,但我怀疑您需要调用类似的

MockitoAnnotations.initMocks(this);

在运行when语句之前。本文显示了一个示例:https://howtodoinjava.com/mockito/mockito-mock-initmocks/