私は非常に奇妙な問題に直面しています。あいまいなMockito - 0マッチャーが予想され、1が記録されました(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));
含まれている私はRAW表現を使用しない場合でも、私に
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.
を与えます。
私はメソッドが含まれている変更不思議な場合:
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
contains(URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
、それが動作します。
コンフィグとRestHelperが両方嘲笑されています
this.restHelperMock = mock(RESTHelper.class);
this.config = mock(MyBMWConfiguration.class);
when(this.config.apiEndpoint()).thenReturn("http://host:port/api");
ApiEndpointとURLは、私はモックたかったに等しい、 それが原因で偽の、私はNullPointerExceptionが得るべきである、ではないでしょう場合でも、嘲笑する しかし、私はここには理想がない。
ありがとうございました。
'hamcrest 'でも' Mockito'の 'contains'と' eq'の両方がありますか? –
私は、 'eq(...)'の呼び出しの間に擬似メソッド( 'this.config.apiEndpoint()')を呼び出している可能性があると思います。モックイトを混乱させるかもしれない別のモックをそこで呼び出すのではなく、完全なURL(http:// host:port/api/my/specific/url /)を単純に入れてみてください。モックの内部状態に依存しているので、 –
@ FlorianSchaetzさん、ありがとう、それは解決策でした。私はこれが起こるとは思わなかった。あなたはanwserを書いてください、私はそれを受け入れることができますか? –