Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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 Mockito无效使用匹配器异常_Java_Unit Testing_Mockito - Fatal编程技术网

Java Mockito无效使用匹配器异常

Java Mockito无效使用匹配器异常,java,unit-testing,mockito,Java,Unit Testing,Mockito,我使用Mockito为以下代码编写单元测试: while (results.hasMore()) { found = true; SearchResult searchResult = (SearchResult) results.next(); Attributes attributes = searchResult.getAttributes(); Attribute attr = a

我使用Mockito为以下代码编写单元测试:

      while (results.hasMore()) {
            found = true;
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            Attribute attr = attributes.get(LdapAttribute.CUSTOMER_GUID.getValue());
            setAttribute(attr);
            if (getAttribute() != null && cust.getCstCustGuid() == null) 
                cust.setCstCustGuid((String) attr.get());
      }
单元测试存根代码:

    Mockito.doReturn(mockCustomer).when(ldap).getLDAPCustomer();
    Mockito.doReturn(mockCtx).when(ldap).getInitialDirContext();
    Mockito.doNothing().when(ldap).setAttribute(Mockito.any(Attribute.class));
    Mockito.doReturn(mockAttribute).when(ldap).getAttribute();
    Mockito.doReturn(mockSearchControls).when(ldap).getSearchControls();
    Mockito.doNothing().when(mockSearchControls).setSearchScope(Mockito.anyInt());
    Mockito.when(mockCtx.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(SearchControls.class))).thenReturn(mockResults);
    Mockito.when(mockResults.hasMore()).thenReturn(true).thenReturn(false);
    Mockito.when(mockResults.next()).thenReturn(mockSearchResults);
    Mockito.when(mockSearchResults.getAttributes()).thenReturn(mockAttributes);
    Mockito.when(mockAttributes.get(Mockito.anyString())).thenReturn(mockAttribute);
    Mockito.when(mockAttribute.get()).thenReturn(Mockito.anyObject());

    Mockito.when(mockCustomer.getCstCustGuid()).thenReturn(Mockito.anyString());
    Mockito.doNothing().when(mockCustomer).setCstCustGuid(Mockito.anyString());
我在该行收到
InvalidUseOfMatchers
异常:

 Mockito.when(mockCustomer.getCstCustGuid()).thenReturn(Mockito.anyString());

请帮助。

您不能在
thenReturn()中使用
Mockito.anyString()
。您只能在使用
Mockito.when()
Mockito.verify()
时使用它。 示例:
Mockito.when(mockCustomer.getSomething(Mockito.anyString())。然后返回(something)

对于您的问题,应该替换行
Mockito.when(mockCustomer.getCstCustGuid())。然后返回(Mockito.anyString())

Mockito.when(mockCustomer.getCstCustGuid())。然后返回(“”)


Mockito.when(mockCustomer.getCstCustGuid())。然后返回(Mockito.mock(String.class))

这是正确的根本原因,您不能使用匹配器返回“any”实例,但您不能也不应该在实际测试中模拟字符串。返回您控制的可模拟类型的模拟就可以了。