java.lang.AssertionError: 意外的方法调用 convertMessagesAsAppropriate(com.Response@1bb35b)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3287126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.AssertionError: Unexpected method call convertMessagesAsAppropriate(com.Response@1bb35b)
提问by Geeta Bapat
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
我的 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);
}
The issue is from line
问题来自行
response = constructResponse(errorCode, errorDesc);
it constructs error response in verifyUser method and passes it to
messageDAOInf.convertMessagesAsAppropriate()
它在 verifyUser 方法中构造错误响应并将其传递给
messageDAOInf.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.
让我知道我应该采取什么方法。谢谢你。
回答by Geeta Bapat
I have found out way of doing it. You need to implement interface org.easymock.IArgumentMatcher
我已经找到了这样做的方法。你需要实现接口 org.easymock.IArgumentMatcher
public class ObjectEquals implements IArgumentMatcher {
/** The expected. */
private Object expected;
/**
* Instantiates a new criterion equals.
*
* @param expected
* the expected
*/
public ObjectEquals(final Object expected) {
this.expected = expected;
}
/* (non-Javadoc)
* @see org.easymock.IArgumentMatcher#matches(java.lang.Object)
*/
public boolean matches(final Object actual) {
return expected.getClass().equals(actual.getClass());
}
/* (non-Javadoc)
* @see org.easymock.IArgumentMatcher#appendTo(java.lang.StringBuffer)
*/
public void appendTo(final StringBuffer buffer) {
buffer.append("buffer(");
}
}
}
and in your test class add method
并在您的测试类中添加方法
/*
* Eq criterion.
*
* @param criterion the criterion
*
* @return the criterion
*/
public static <T> T eqCriterion(final Class<T> className, Object object) {
EasyMock.reportMatcher(new ObjectEquals(object));
return null;
}
Now while passing to easymock use method eqCriterion at line
现在在传递给 easymock 时使用方法 eqCriterion 在行
EasyMock.expect(messageDAOInf.convertMessagesAsAppropriate(
response)).andReturn(response);
In short replace above line with
简而言之,将上面的行替换为
EasyMock.expect(messageDAOInf.convertMessagesAsAppropriate(
eqCriterion(Response.class, response))).andReturn(response);
This way it will use this mocked response instance instead of one generated by actual code.
这样,它将使用这个模拟的响应实例,而不是由实际代码生成的响应实例。
回答by DoctorRuss
Your initial code expects that convertMessagesAsAppropriate
will be called with the exact instanceof Response
that you created in the test: obviously it will not do that.
您最初的代码预计convertMessagesAsAppropriate
将有确切的被称为实例的Response
,你在测试中创造了:显然不会那么做的。
The correction you've made is essentially the same as using the built-in EasyMock.anyObject()
method which will allow any Response
instance. If that's all you want to check in your unit test, that's fine. Alternatively you can add extra logic into your ArgumentMatcher
to prove that the Response
that is passed as an argument really is an ErrorResponse, or Capture
the response and examine it in your test. This all depends on your level of testing :-)
您所做的更正本质上与使用EasyMock.anyObject()
允许任何Response
实例的内置方法相同。如果这就是您要在单元测试中检查的全部内容,那很好。或者,您可以向您添加额外的逻辑,ArgumentMatcher
以证明Response
作为参数传递的 确实是 ErrorResponse 或Capture
响应并在您的测试中检查它。这一切都取决于您的测试水平:-)