JMockit 没有。 API 次通话
JMockit no. of times API call
我正在测试 doSomething
方法是否在发生异常时再次在 catch 块中被调用。如何验证 'doSomething' 是否被调用了两次?
Class待测:
@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);
}
}
我正在测试 doSomething
方法是否在发生异常时再次在 catch 块中被调用。如何验证 'doSomething' 是否被调用了两次?
Class待测:
@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);
}
}