【发布时间】:2016-01-05 14:25:18
【问题描述】:
我正在尝试模拟方法的返回值,但我得到了NotAMockException。
@InjectMocks
private MyService myService;
@Mock
private OtherServiceUsedInMyServiceAsAutowired otherServiceUsedInMyServiceAsAutowired;
在MyService 内部,我有一个名为myMethod() 的方法,我想在调用此方法时返回虚拟对象。
doReturn(someDummyObject).when(myService).myMethod(any(), any(), any());
那时我得到了错误。我究竟做错了什么?完全错误:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
【问题讨论】:
-
@InjectMocks 注解实际上是在创建模拟吗?
标签: java spring testing mocking mockito