[英]Mockito invalid use of Matchers exception
我有以下代碼,我正在使用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());
請幫忙。
你不能在Mockito.anyString()
使用thenReturn()
。 您只能在使用Mockito.when()
或Mockito.verify()
時使用它。 示例: Mockito.when(mockCustomer.getSomething(Mockito.anyString())).thenReturn(something);
對於您的問題,您應該替換Mockito.when(mockCustomer.getCstCustGuid()).thenReturn(Mockito.anyString());
通過
Mockito.when(mockCustomer.getCstCustGuid()).thenReturn("");
要么
Mockito.when(mockCustomer.getCstCustGuid()).thenReturn(Mockito.mock(String.class));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.