private boolean isEmpty(Object[] array) {
if (array == null || array.length == 0)
return true;
for (int i = 0; i < array.length; i++) {
if (array[i] != null)
return false;
}
return true;
}
@Test
public void testIsEmpty() {
//where is an instance of the class whose method isEmpty() I want to test.
try {
Object[] arr = null;
assertTrue((Boolean)(Whitebox.invokeMethod(where, "isEmpty", new Class<?>[]{Object[].class}, arr)));
arr = new Object[0];
assertTrue((Boolean)(Whitebox.invokeMethod(where, "isEmpty", new Class<?>[]{Object[].class}, arr)));
arr = new Object[]{null, null};
assertTrue((Boolean)(Whitebox.invokeMethod(where, "isEmpty", new Class<?>[]{Object[].class}, arr)));
arr = new Object[]{1, 2};
assertFalse((Boolean)(Whitebox.invokeMethod(where, "isEmpty", new Class<?>[]{Object[].class}, arr)));
} catch (Exception e) {
fail(e.getMessage());
}
}问题: java.lang.AssertionError:错误的参数数
研究: 1.最初,我尝试了:invokeMethod(对象测试,字符串methodToExecute,Object.(论据)
它在第2、第3和第4 invokeMethod()中失败。(错误:在给定参数下找不到的方法)
我认为这可能是因为PowerMock无法推断出正确的方法;因此,我转而使用invokeMethod(对象测试,字符串methodToExecute,Class[] argumentTypes,Object.(论据)
任何帮助都是非常感谢的。谢谢!
发布于 2012-11-01 21:49:24
我认为它应该通过将参数转换为Object来工作,以便使Java将其视为单个参数,而不是与Object...对应的一系列参数。
Whitebox.invokeMethod(where, "isEmpty", new Class<?>[]{Object[].class}, (Object) arr);测试用例:
public static void main(String[] args) {
foo(new Object[] {"1", "2"}); // prints arg = 1\narg=2
foo((Object) (new Object[] {"1", "2"})); // prints args = [Ljava.lang.Object;@969cccc
}
private static void foo(Object... args) {
for (Object arg : args) {
System.out.println("arg = " + arg);
}
}https://stackoverflow.com/questions/13185784
复制相似问题