Mockito: InvalidUseOfMatchersException
大家好,我是威5,更多精彩关注wx公众号:
以下是本文正式内容,供参考:
异常报错信息:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 4 matchers expected, 3 recorded: -> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java:136) -> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java:136) -> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java:136) This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(), eq("String by matcher")); For more info see javadoc for Matchers class.
报错的测试代码:
Mockito.when(baseDaoRead.queryForObject(Mockito.anyString(), Mockito.anyMap(), String.class)).thenReturn("123.11");
改正确之后的测试代码:(主要是String.class不是使用Matchers传参)
Mockito.when(baseDaoRead.queryForObject("industry.getMaxYestPayIdx", params, String.class)).thenReturn("123.11");
或者这样也是正确的:
Mockito.when(baseDao.queryForList(Mockito.anyString(), Mockito.anyMap())).thenReturn(list3);
主要差异是对传入的字符串做了Matchers.eq()动作
原因:使用Matchers不能只针对部分参数,所有参数都应该采用Matchers