mam*_*uoc 2 java spring unit-testing mocking mockito
我有:
Map<String, String> vars = new HashMap<String, String>();
String r = restOperations.getForObject(url, String.class, vars);
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我尝试模拟RestOperations:
@Mock
RestOperations restOperations;
when(restOperations.getForObject(Matchers.anyString(), Matchers.eq(String.class), Matchers.notNull())).thenReturn("ok");
Run Code Online (Sandbox Code Playgroud)
这永远不会被触发.
anyboy看到为什么?
我认为问题在于方法重载.getForObject有两个重载版本带有三个参数:
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables);
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables);
Run Code Online (Sandbox Code Playgroud)
实际上,当你想要模拟第一个时,你正在嘲笑第二个.为了帮助静态方法解析notNull()显式地转换匹配:
when(restOperations.getForObject(
Matchers.anyString(), Matchers.eq(String.class), (Map)Matchers.notNull())
).thenReturn("ok");
Run Code Online (Sandbox Code Playgroud)
甚至更好,使用anyMap():
when(restOperations.getForObject(
Matchers.anyString(), Matchers.eq(String.class), Matchers.anyMap())
).thenReturn("ok");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6565 次 |
| 最近记录: |