简体   繁体   English

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

[英]mockito test error Invalid use of argument matchers

I have been getting writing unit tests for a method that invokes jdbcTemplate.query and returns some data.我一直在为调用jdbcTemplate.query并返回一些数据的方法编写单元测试。 It doesn't seems to be working and throwing exception.它似乎没有工作并抛出异常。

Here's the code.这是代码。

@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);
    }

Error message错误信息

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"));

Just for your information that method newDealDaoImpl.getResultSetExtractor takes 3 arguments<String, String, String>.仅供参考, newDealDaoImpl.getResultSetExtractor方法采用 3 个参数<String, String, String>。

The problem is that you are using argument matchers:问题是您正在使用参数匹配器:

Mockito.anyString()

on an object that is not managed by Mockito (mock, spy etc.)在不受 Mockito 管理的 object 上(模拟、间谍等)

Try to pass an empty String or other random value to your:尝试将空字符串或其他随机值传递给您的:

newDealDaoImpl.getResultSetExtractor(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mockito 错误:.InvalidUseOfMatchersException:参数匹配器的使用无效 - Mockito Error :.InvalidUseOfMatchersException: Invalid use of argument matchers Java Mockito useConstructor withSettings,错误参数匹配器的无效使用 - Java Mockito useConstructor withSettings, error Invalid use of argument matchers 如何修复 Mockito Invalid use of argument matchers 错误 - How to fix Mockito Invalid use of argument matchers error Mockito中的anyString()抛出参数匹配器的无效使用 - anyString() in mockito is throwing an invalid use of argument matchers Java Mockito 参数匹配器的无效使用 - Java Mockito Invalid use of argument matchers Mockito varargs参数匹配器的无效使用 - Mockito varargs Invalid use of argument matchers Mockito 单元测试:参数匹配器的使用无效 - Mockito unit testing: Invalid use of argument matchers Mockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException:无效使用参数匹配器! 预计有0个符合条件的记录,有1个记录: - Mockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 recorded: org.mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的无效使用 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers Mockito - 无效使用匹配器异常 - Mockito - Invalid use of Matchers Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM