java.lang.AssertionError:意外的方法调用convertMessagesAsAppropriate(com.Response@1bb35b)
需要帮助是决定需要采取什么方法来测试下面的代码
我有一个名为
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: 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)
让我知道我应该采取什么方法。 谢谢。
Need help is deciding what approach needs to be taken to test below piece of code
I have one method called
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;
}
My EasyMock code is here
/** 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);
}
The issue is from line
response = constructResponse(errorCode, errorDesc);
it constructs error response in verifyUser method and passes it tomessageDAOInf.convertMessagesAsAppropriate()
But with easy mock it passes some other instance (mocked one) and hence failes with 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)
Let me know what approach I should take.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的初始代码期望使用您在测试中创建的
Response
的确切实例来调用convertMessagesAsAppropriate
:显然它不会这样做。您所做的更正本质上与使用内置
EasyMock.anyObject()
方法相同,该方法将允许任何Response
实例。如果这就是您想要在单元测试中检查的全部内容,那没问题。或者,您可以在ArgumentMatcher
中添加额外的逻辑,以证明作为参数传递的Response
确实是 ErrorResponse,或者Capture
响应并在你的测试中检查它。这一切都取决于您的测试水平:-)Your initial code expects that
convertMessagesAsAppropriate
will be called with the exact instance ofResponse
that you created in the test: obviously it will not do that.The correction you've made is essentially the same as using the built-in
EasyMock.anyObject()
method which will allow anyResponse
instance. If that's all you want to check in your unit test, that's fine. Alternatively you can add extra logic into yourArgumentMatcher
to prove that theResponse
that is passed as an argument really is an ErrorResponse, orCapture
the response and examine it in your test. This all depends on your level of testing :-)我已经找到了方法。
您需要实现接口 org.easymock.IArgumentMatcher
}
并在您的测试类中添加方法,
现在在传递给 easymock 时使用方法 eqCriterion 行
简而言之,将上面的行替换为
这样,它将使用此模拟响应实例,而不是由实际代码生成的响应实例。
I have found out way of doing it.
You need to implement interface org.easymock.IArgumentMatcher
}
and in your test class add method
Now while passing to easymock use method eqCriterion at line
In short replace above line with
This way it will use this mocked response instance instead of one generated by actual code.