Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.mockito.exceptions.misusing.invalidUseofMatchers异常错位参数匹配器_Java_Unit Testing_Mockito - Fatal编程技术网

Java org.mockito.exceptions.misusing.invalidUseofMatchers异常错位参数匹配器

Java org.mockito.exceptions.misusing.invalidUseofMatchers异常错位参数匹配器,java,unit-testing,mockito,Java,Unit Testing,Mockito,我得到以下异常 org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected here: You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get

我得到以下异常

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected here:

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))
对于代码行

@Mock    
Template templateParser = new TemplateParser();
when(templateParser.getLayoutIdsFromTemplate(any(HashMap.class)))
                           .thenReturn(Arrays.asList("id1", "id2"));
我正在模拟的函数如下所示

public class TemplateParser {
    public List<String> getLayoutIdsFromTemplate(HashMap<String, Object> parsedTemplate)
    {
        List<String> listOfLayoutIds = new ArrayList<String>();

        //Loading listOfLayoutIds here

        return listOfLayoutIds;
    }
}
公共类TemplateParser{
公共列表getLayoutIdsFromTemplate(HashMap parsedTemplate)
{
List ListofLayoutId=new ArrayList();
//正在此处加载LayoutID列表
返回layoutids列表;
}
}

有人能告诉我问题出在哪里吗?

您的模拟方法
getLayoutIdsFromTemplate
正在接收一个键入的
HashMap
作为参数,而不是一个简单的
HashMap
。这是导致
无效使用MatcherException
的原因

如果您可以进行一点重构,您可以通过以下方式解决它:

1) 将方法更改为接收
映射
,而不是
哈希映射


希望有帮助。

看起来您可能错误地使用了
@Mock
注释。你能展示你的完整的测试课程吗?(或最低代表性版本)?
   public List<String> getLayoutIdsFromTemplate(Map<String, Object> parsedTemplate) {
      ...
   }
when(templateParser.getLayoutIdsFromTemplate(anyMapOf(String.class, Object.class))).thenReturn(Arrays.asList("id1", "id1"));