Java JMockit API调用次数
我正在测试当异常发生时,catch块中是否再次调用了Java JMockit API调用次数,java,jmockit,Java,Jmockit,我正在测试当异常发生时,catch块中是否再次调用了doSomething方法。如何验证“doSomething”是否被调用两次 待测类别: @Service public class ClassToBeTested implements IClassToBeTested { @Autowired ISomeInterface someInterface; public String callInterfaceMethod() { String respObj; try{ re
doSomething
方法。如何验证“doSomething”是否被调用两次
待测类别:
@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);
}
}
以下方面应起作用:
public class ClassToBeTestedTest
{
@Tested ClassToBeTested classUnderTest;
@Injectable ISomeInterface mockSomeInterface;
@Test
public void exampleTest() {
String mockResp = "mockResp";
new Expectations() {{
// There is *one* expectation, not two:
mockSomeInterface.doSomething(anyString, anyString);
times = 2; // specify the expected number of invocations
// specify one or more expected results:
result = new MyException();
result = mockResp;
}};
String resp = classUnderTest.callInterfaceMethod();
assertEquals(mockResp, resp);
}
}
这个怎么样?检查此问答。。。本质上,它是verify(mockObject,times(3)).someMethod(“恰好被调用了三次”)@玩具它部分工作。我必须在预期的情况下删除第二个模拟调用。我可以验证调用计数,但现在我无法验证是否从catch块得到了预期的响应。如何验证两者together@DV88这是Mockito的例子,我在JMockit中寻找一些东西。