Java Mockito-如何模拟参数数量不确定的方法

2024-04-06

我尝试使用 Mockito 来模拟getDeclaredMethod()爪哇的。 但该方法的参数是不确定的。如何模拟这样的方法?

public Method getDeclaredMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException {
    throw new RuntimeException("Stub!");
}

Use ArgumentMatchers.any() https://static.javadoc.io/org.mockito/mockito-core/2.2.7/org/mockito/ArgumentMatchers.html#any()

匹配任何内容,包括 null 和 varargs。

Example

when(mockedObject.getDeclaredMethod(anyString(),any())).thenReturn("element");

在你的情况下

when(mockedObject.getDeclaredMethod(anyString(), (Class<?>)any())).thenReturn("element");

并且任意变量() https://static.javadoc.io/org.mockito/mockito-core/2.2.7/org/mockito/ArgumentMatchers.html#anyVararg()但这已被弃用。自 2.1.0 起

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java Mockito-如何模拟参数数量不确定的方法 的相关文章

随机推荐