Reg*_*ald 13 java mocking mockito
Mockito "The method when(T) in the type Stubber is not applicable for the arguments (void)"为我正在嘲笑的课程抛出错误,无法弄清楚原因.
有问题的代码是:
Mockito.when(mockObject.myMethod(Mockito.any(MyExecutionContext.class))).thenReturn(value);
Run Code Online (Sandbox Code Playgroud)
我知道有人问过类似的问题,但是如果有人可以为此解释一下解决方案或者指出我正确的方向,我将非常感激
Reg*_*ald 32
解:
Mockito.doReturn(value)
.when(mockObject)
.myMethod(Mockito.any(MyExecutionContext??.class))
Run Code Online (Sandbox Code Playgroud)
小智 6
您正在模拟的方法(在上面的示例中:mockObject.myMethod)的返回类型很可能是 VOID。
如果方法的返回类型是“void”,则使用以下格式:
doThrow(new PersistenceException("Exception occured")).when(mockObject).myMethod(any());
// Note that we can never return a value when return type is void.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22418 次 |
| 最近记录: |