Need help, decides which approach to take in order to test the code snippet below.
I have one method called
private messageDAOInf messageDAO;
public Response verifyUser(Request request) {
Response response = null;
if (someCondition) {
} else {
response = constructResponse(errorCode, errorDesc);
}
response = messageDAOInf
.convertMessagesAsAppropriate(response);
return response;
}
My EasyMock code is here
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);
}
The problem is with the line
response = constructResponse(errorCode, errorDesc);
it creates an error response in the verifyUser method and passes it
messageDAOInf.convertMessagesAsAppropriate()
But with a light broker, it passes some other instance (mocks it) and, therefore, fails with an error
java.lang.AssertionError:
Unexpected method call convertMessagesAsAppropriate (*** Response @ 1bb35b ***):
convertMessagesAsAppropriate (*** Response @ 1b5d2b2 ***): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:56), .
.