[英]Mockito - 0 Matchers Expected, 2 Recorded (InvalidUseOfMatchersException)
[英]Ambiguous Mockito - 0 Matchers Expected, 1 Recorded (InvalidUseOfMatchersException)
我面臨一個非常奇怪的問題。
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
eq(this.config.apiEndpoint() + URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
甚至包含
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
contains(this.config.apiEndpoint() + URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
給我
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.
即使我不使用RAW表達式。
奇怪的是,如果我將contains方法更改為:
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
contains(URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
省略端點,它的工作原理。
Config和RestHelper都被嘲笑:
this.restHelperMock = mock(RESTHelper.class);
this.config = mock(MyBMWConfiguration.class);
when(this.config.apiEndpoint()).thenReturn("http://host:port/api");
帶有ApiEndpoint的URL等於我想要模擬的內容,即使它不是,我也應該得到一個NullpointerException,因為它是虛假的模擬。 但在這里我沒有任何想法。
謝謝您的回答。
問題似乎是你在eq ( ... )
調用期間調用了一個模擬方法this.config.apiEndpoint()
。 嘗試簡單地將完整的URL放在那里(host:port / api / my / specific / url),而不是在那里調用另一個模擬,這可能會混淆Mockito,因為它依賴於內部狀態進行模擬。
說實話,我不是那么深入Mockito,我可以解釋為什么會發生這種情況,但有一天我可能會嘗試調試它;-)
編輯:奇怪的是,我似乎無法用更簡單的測試用例重現它。 這里似乎有更多,而不是滿足眼睛。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.