mockito 测试错误参数匹配器的使用无效

mockito test error Invalid use of argument matchers

我一直在为调用 jdbcTemplate.query 和 returns 一些数据的方法编写单元测试。它似乎没有工作并抛出异常。

这是代码。

@Test
    public void NewDealDaoGetClientOwnershipValuesTest() {
        List<OptionView> optionViews = new ArrayList<OptionView>();
        optionViews.add(new OptionView("one", "two"));

        when(jdbcTemplate.query("<some sql query>", newDealDaoImpl.getResultSetExtractor(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))).thenReturn(optionViews);
        assertEquals(newDealDaoImpl.getClientOwnershipValues(), optionViews);
    }

错误信息

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 3 recorded.
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"));

仅供参考,方法 newDealDaoImpl.getResultSetExtractor 需要 3 个参数

问题是您正在使用参数匹配器:

Mockito.anyString()

在不受 Mockito 管理的对象上(模拟、间谍等)

尝试将空字符串或其他随机值传递给您的:

newDealDaoImpl.getResultSetExtractor(...)