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

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

问题描述

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

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值,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天全站免登陆