【发布时间】:2017-06-29 19:53:26
【问题描述】:
我尝试在 MyClass 类中模拟以下方法 foo:
private Class<?> foo(final String str) throws ClassNotFoundException{
return Class.forName(str);
}
所以我写了:
MyClass pl = PowerMockito.spy(new MyClass());
try {
PowerMockito.when(pl, PowerMockito.method(MyClass.class, "foo", String.class))
.withArguments(anyString())
.thenReturn(Boolean.TRUE.getClass());
} catch (Exception e) {
e.printStackTrace(); // catch ClassNotFoundException
}
它不起作用并抛出异常java.lang.reflect.InvocationTargetException。
但是,如果我在 foo 方法中将 return Class.forName(str) 替换为 return Boolean.TRUE.getClass(); - 一切正常。
如何告诉PowerMockito 调用方法foo 失败时抛出异常?
【问题讨论】: