我正在测试一个函数,该函数在异常时使用不同的参数重试。以下是伪代码。在junit中测试引发异常的方法
class Myclass {
public void load(input)
try {
externalAPI.foo(input);
} catch(SomeException e) {
//retry with different parameters
externalAPI.foo(input,input2);
}
如何使用junit通过嘲笑externalAPI来测试上面的代码。
@Test
public void testServiceFailover(){
m_context.checking(new Expectations() {{
allowing (mockObjExternalAPI).foo(with(any(String.class)));
will (throwException(InvalidCustomerException));
allowing (mockObjExternalAPI).foo(with(any(String.class),with(any(String.class)));
will (returnValue(mockResult));
}});
}
但上面的测试失败,说“试图抛出SomeException异常从一个方法(从foo())抛出没有例外”。但实际上,foo方法在其方法签名中提到了SomeException。
我该如何写函数foo的junit?
什么模拟框架您使用的? – walters 2012-09-21 09:39:05