我正在測試一個函數,該函數在異常時使用不同的參數重試。以下是僞代碼。在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