Mockito: Match any String except one [duplicate]

爱⌒轻易说出口 提交于 2019-12-10 01:57:02

问题


How can I write a matcher using Mockito that matches any string except a specific one?

I have tried using some hamcrest matchers to negate and combine other matchers, but the hamcrest matchers all return values of type Matcher<T> which dont work very well with Mockito matchers.


回答1:


The solution I used:

argThat(not("ExceptionString"))

Where argThat is a Mockito matcher,
and not is a Hamcrest Matcher




回答2:


Just point that with Mockito you can also use AdditionalMatchers and ArgumentMatchers

import static org.mockito.AdditionalMatchers.not;
import static org.mockito.ArgumentMatchers.eq;

//anything but not "ejb"    
mock.someMethod(not(eq("ejb")));

According its documentation:

Example of using logical and(), not(), or() matchers:

//anything but not "ejb"
mock.someMethod(not(eq("ejb")));

There is more info in this other SO question

Hope it helps



来源:https://stackoverflow.com/questions/30107476/mockito-match-any-string-except-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!