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

问题描述 投票:0回答:1

我有这样的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是一个接口

这可能是什么问题?

mockito
1个回答
0
投票

你有没有初始化嘲笑?这将有助于看到完整的代码发布,但我怀疑你需要调用类似的东西

MockitoAnnotations.initMocks(this);

在你运行你的when声明之前。本文展示了一个例子:https://howtodoinjava.com/mockito/mockito-mock-initmocks/

© www.soinside.com 2019 - 2024. All rights reserved.