Dav*_*aci 7 java junit mocking mockito
我面临一个非常奇怪的问题.
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
eq(this.config.apiEndpoint() + URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
Run Code Online (Sandbox Code Playgroud)
甚至包含
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
contains(this.config.apiEndpoint() + URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
Run Code Online (Sandbox Code Playgroud)
给我
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.
Run Code Online (Sandbox Code Playgroud)
即使我不使用RAW表达式.
奇怪的是,如果我将contains方法更改为:
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
contains(URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
Run Code Online (Sandbox Code Playgroud)
省略端点,它的工作原理.
Config和RestHelper都被嘲笑:
this.restHelperMock = mock(RESTHelper.class);
this.config = mock(MyBMWConfiguration.class);
when(this.config.apiEndpoint()).thenReturn("http://host:port/api");
Run Code Online (Sandbox Code Playgroud)
带有ApiEndpoint的URL等于我想要模拟的内容,即使它不是,我也应该得到一个NullpointerException,因为它是虚假的模拟.但在这里我没有任何想法.
谢谢您的回答.
问题似乎是你在调用this.config.apiEndpoint()期间调用了一个模拟方法eq ( ... ).尝试简单地将完整的URL放在那里(host:port/api/my/specific/url),而不是在那里调用另一个模拟,这可能会混淆Mockito,因为它依赖于内部状态进行模拟.
说实话,我不是那么深入Mockito,我可以解释为什么会发生这种情况,但有一天我可能会尝试调试它;-)
编辑:奇怪的是,我似乎无法用更简单的测试用例重现它.这里似乎有更多的东西比满足眼睛.
| 归档时间: |
|
| 查看次数: |
3226 次 |
| 最近记录: |