我尝试使用Mockito模拟getDeclaredMethod()Java。但是此方法的参数不确定。如何嘲笑这种方法?
public Method getDeclaredMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException {
throw new RuntimeException("Stub!");
}
Run Code Online (Sandbox Code Playgroud)
匹配任何内容,包括 null 和 varargs。
例子
when(mockedObject.getDeclaredMethod(anyString(),any())).thenReturn("element");
Run Code Online (Sandbox Code Playgroud)
在你的情况下
when(mockedObject.getDeclaredMethod(anyString(), (Class<?>)any())).thenReturn("element");
Run Code Online (Sandbox Code Playgroud)
还有anyVararg(),但已弃用。自 2.1.0 起
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |