我想這樣做:問題用「OR」匹配器。當謂詞的Mockito
在一個when(myObject.getEntity(1l,usr.getUserName()).thenReturn(null);
when(myObject.getEntity(1l,Constants.ADMIN)).thenReturn(null);
符合的匹配。所以,我有這樣的代碼:
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.AdditionalMatchers.*;
[...]
User usr = mock(usr);
[...]
when(myObject.getEntity(
eq(1l),
or(eq(usr.getUserName()),eq(Constants.ADMIN))
)
).thenReturn(null);
但是,當我使用或者匹配,JUnit的失敗:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded.
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"));
For more info see javadoc for Matchers class.
at blah.blah.MyTestClass.setup(MyTestClass:The line where I use the when & or matcher)
... more stacktrace ...
什麼我'做錯了什麼?
謝謝!
這個變化背後的動機是什麼?在考慮可讀性和可維護性時,它看起來起反作用。 – atomman
是Mockito'AdditionalMatchers'類(它應該工作)的'or'方法還是你正在處理Hamcrest匹配器(它不會以這種方式工作)? –
是的,是org.mockito.AdditionalMatchers類中的「or()」。 @Aomom ...沒有什麼嚴肅的動機。只有我想學習如何做到這一點:) – Nullpo