2
我正在测试如果doSomething
方法再次在发生异常时在catch块中调用。如何验证'doSomething'是否被调用过两次?时间API调用
类来进行测试:
@Service
public class ClassToBeTested implements IClassToBeTested {
@Autowired
ISomeInterface someInterface;
public String callInterfaceMethod() {
String respObj;
try{
respObj = someInterface.doSomething(); //throws MyException
} catch(MyException e){
//do something and then try again
respObj = someInterface.doSomething();
} catch(Exception e){
e.printStackTrace();
}
return respObj;
}
}
测试用例:
public class ClassToBeTestedTest
{
@Tested ClassToBeTested classUnderTest;
@Injectable ISomeInterface mockSomeInterface;
@Test
public void exampleTest() {
String resp = null;
String mockResp = "mockResp";
new Expectations() {{
mockSomeInterface.doSomething(anyString, anyString);
result = new MyException();
mockSomeInterface.doSomething(anyString, anyString);
result = mockResp;
}};
// call the classUnderTest
resp = classUnderTest.callInterfaceMethod();
assertEquals(mockResp, resp);
}
}
这个怎么样? http://stackoverflow.com/questions/7700965/equivalent-of-times-in-jmockit – toy
查看本问与答。 http://stackoverflow.com/questions/14889951/how-to-verify-a-method-is-called-two-times-with-mockito-verify ...实质上,它是验证(mockObject,times(3)) .someMethod(“被称为三次”); – DV88
@toy它部分地工作。我不得不在预期下删除第二个模拟电话。我可以验证呼叫计数,但现在我无法验证我是否从catch block得到了预期响应。如何一起验证 – Pankaj