最近在使用mock进行单元测试的时候,遇到了下面的问题:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded:
-> at com.xxx.xxx.clearing.mock.AccountUpdateMock.execute(AccountUpdateMock.java:38)
-> at com.xxx.xxx.clearing.mock.AccountUpdateMock.execute(AccountUpdateMock.java:38)
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.
看提示是告诉我们,如果有一个参数用了匹配,那么所有的参数都必须采用匹配。
但是我的用法是:
public List<Account> getUpdateAccountList(Date startDate, Date endDate) { AccountQuery param = new AccountQuery(); param.setUpdateTimeStart(startDate); param.setUpdateTimeEnd(endDate); return accountService.updateAccountList(param);} 然后mock:
Mockito.when(prepare.getUpdateAccountList(Mockito.any(), Mockito.any())).thenReturn(accMods);两个参数均是用的any匹配,还是报上述错误。 后面有两种办法解决:方法1:定义一个 Date date = Mockito.any();
Mockito.when(prepare.getUpdateAccountList(date, date)).thenReturn(accMods);
方法2:
public List<Account> getUpdateAccountList(AccountQuery param) {
return accountService.updateAccountList(param);}
初步怀疑是,因为两个参数都是Date类型,两个any匹配出错。
原文地址:https://www.cnblogs.com/jylsgup/p/11154111.html
时间: 2024-10-09 02:38:22