Java org.mockito.exceptions.misusing.InvalidUseOfMatchersException:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22164787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:01:05  来源:igfitidea点击:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:

javajunitmockitomatcher

提问by user2375298

My method in the service and test class :

我在服务和测试类中的方法:

public void updateSubModuleOrder(Long[] data, Long moduleSysId, Long userId) {
    try {
        for (int i = 0; i < data.length; i++) {
            SubModule subModule=new SubModule();

            int temp = i + 1;
            userSubmodule.setDsplySeq(temp);
            userSubModuleDao.saveOrUpdate(userSubmodule);
@Test
public void testupdateSubModuleOrder(){
    UserModuleServiceImpl userModuleServiceImpl = new UserModuleServiceImpl();
    UserSubModuleDao userSubModuleDao = mock(User//set the required param ,some code here//
    UserSubModuleId userSubModuleId=new UserSubModuleId();
    //some code//
    when(userSubModuleDao.findById((any(UserSubModuleId.class)),false)).thenReturn(userSubModule);
    when(userSubModuleDao.saveOrUpdate(any(UserSubModule.class))).thenReturn(null);
    userModuleServiceImpl.updateSubModuleOrder(data, moduleSysId, userId);

};*

the error I get is

我得到的错误是

FAILED: testupdateSubModuleOrder
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.TestUserModuleServiceImpl.testupdateSubModuleOrder(TestUserModuleServiceImpl.java:267)

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

the method findbyIDis a baseDao method which my dao extends . It is not a final or static but still I'm getting this problem.

该方法findbyID是我的 dao 扩展的 baseDao 方法。它不是最终的或静态的,但我仍然遇到这个问题。

采纳答案by Jon Skeet

You either have to specify nomatchers, or allthe arguments need matches. So this:

你要么必须指定匹配器,或者所有的参数需要匹配。所以这:

when(userSubModuleDao.findById((any(UserSubModuleId.class)),false))

should be:

应该:

when(userSubModuleDao.findById(any(UserSubModuleId.class), eq(false)))

(I've removed the redundant brackets from around the anycall.)

(我已经从any调用中删除了多余的括号。)

From the Matchersdocumentation:

Matchers文档

Warning:

If you are using argument matchers, all argumentshave to be provided by matchers.

警告:

如果您使用参数匹配器,则所有参数都必须由匹配器提供。