mockito 中的测试失败消息:参数不同!通缉: [英] Test Failure Message in mockito : Argument(s) are different! Wanted:

查看:23
本文介绍了mockito 中的测试失败消息:参数不同!通缉:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 JUnit 中测试一个 Restful 端点,并在作为参数存在于 save 方法中的列表,

I am testing a Restful endpoint in my JUnit and getting an exception as below in the list which is present as an argument inside the save method,

**"Argument(s) are different! Wanted:"** 
save(
"121",
[com.domain.PP@6809cf9d, 
com.domain.PP@5925d603]
);
Actual invocation has different arguments:
save(
"121",
[com.domain.PP@5b6e23fd,  
com.domain.PP@1791fe40]
 ); 

当我调试代码时,代码在下面的 verify 行中断并抛出以上异常.看起来像保存中testpPList"中的参数方法不同.我不知道当我在我的JUNit 正确,然后调用 RestFul URL.

When I debugged the code, the code broke at the verify line below and threw the above exception. Looks like the arguments inside the "testpPList" within the save method is different. I dont know how it becomes different as I construct them in my JUNit properly and then RestFul URL is invoked.

请求您提供宝贵意见.谢谢.

Requesting your valuable inputs. Thanks.

代码:

@Test
public void testSelected() throws Exception {
    mockMvc.perform(put("/endpointURL")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(testObject)))
        .andExpect(status().isOk());
    verify(programServiceMock, times(1)).save(id, testpPList);
    verifyNoMoreInteractions(programServiceMock);
}

控制器方法:

@RequestMapping(value = "/endpointURL", method = RequestMethod.PUT)
public @ResponseBody void uPP(@PathVariable String id, @RequestBody List<PPView> pPViews) {
    // Code to construct the list which is passed into the save method below
    save(id, pPList);
}

推荐答案

实现Object#equals(Object)可以通过相等比较来解决.尽管如此,有时您正在验证的对象无法更改,或者其 equals 函数无法实现.对于这种情况,建议使用 org.mockito.Matchers#refEq(T value, String... excludeFields).所以你可以使用类似的东西:

Implementing the Object#equals(Object) can solve it by the equality comparison. Nonetheless, sometimes the object you are validating cannot be changed or its equals function can not be implemented. For such cases, it's recommended using org.mockito.Matchers#refEq(T value, String... excludeFields). So you may use something like:

verify(programServiceMock, times(1)).save(id, refEq(testpPList));

只需用 refEq 包装参数即可解决问题.

Just wrapping the argument with refEq solves the problem.

这篇关于mockito 中的测试失败消息:参数不同!通缉:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆