需要帮助是决定需要采取什么方法来测试下面的代码
我有一种方法叫做
private messageDAOInf messageDAO;
public Response verifyUser(Request request) {
Response response = null;
if (someCondition) {
/* -----------Some processing here---------- */
} else {
response = constructResponse(errorCode, errorDesc);
}
// Do more processing with messages from response
response = messageDAOInf
.convertMessagesAsAppropriate(response);
return response;
}
我的 EasyMock 代码在这里
/** The message dao inf. */
private MessageDAOInf messageDAOInf;
private VerifyUserService verifyUserServiceI;
@Before
public void setUp() throws Exception {
messageDAOInf = EasyMock.createMock(MessageDAOInf.class);
verifyUserService = new VerifyUserService();
verifyUserService.setMessageDAOInf(messageDAOInf);
}
@Test
public void testErrorResponse() {
Request request = loadRequest();
Response response = constructErrorResponse();
EasyMock.expect(messageDAOInf.convertMessagesAsAppropriate(
response)).andReturn(response);
EasyMock.replay(messageDAOInf);
Response response2 = verifyUserService.verifyUser(request);
assertFailedResponse(response2);
}
问题是从线
response = constructResponse(errorCode, errorDesc);
它在 verifyUser 方法中构造错误响应并将其传递给
messageDAOInf.convertMessagesAsAppropriate()
但是通过简单的模拟,它会传递一些其他实例(模拟一个),因此失败并出现错误
java.lang.AssertionError: 意外的方法调用 convertMessagesAsAppropriate(***Response@1bb35b***): convertMessagesAsAppropriate(***Response@1b5d2b2***):预期:1,实际:0 在 org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29) 在 org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:56)
让我知道我应该采取什么方法。谢谢你。