java.lang.AssertionError:意外的方法调用convertMessagesAsAppropriate(com.Response@1bb35b)

发布于 2024-09-10 17:14:57 字数 1907 浏览 7 评论 0原文

需要帮助是决定需要采取什么方法来测试下面的代码

我有一个名为

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 to
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-09-17 17:14:57

您的初始代码期望使用您在测试中创建的 Response 的确切实例来调用 convertMessagesAsAppropriate:显然它不会这样做。

您所做的更正本质上与使用内置 EasyMock.anyObject() 方法相同,该方法将允许任何 Response 实例。如果这就是您想要在单元测试中检查的全部内容,那没问题。或者,您可以在 ArgumentMatcher 中添加额外的逻辑,以证明作为参数传递的 Response 确实是 ErrorResponse,或者Capture 响应并在你的测试中检查它。这一切都取决于您的测试水平:-)

Your initial code expects that convertMessagesAsAppropriate will be called with the exact instance of Response 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 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 :-)

难以启齿的温柔 2024-09-17 17:14:57

我已经找到了方法。
您需要实现接口 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(");
}

}

并在您的测试类中添加方法,

/*
     * 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;
    }

现在在传递给 easymock 时使用方法 eqCriterion 行

EasyMock.expect(messageDAOInf.convertMessagesAsAppropriate(         
        response)).andReturn(response);         

简而言之,将上面的行替换为

EasyMock.expect(messageDAOInf.convertMessagesAsAppropriate(         
        eqCriterion(Response.class, response))).andReturn(response);         

这样,它将使用此模拟响应实例,而不是由实际代码生成的响应实例。

I have found out way of doing it.
You need to implement interface 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.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文