有人能告诉我如何模拟和编写Mockito.when()方法吗?
@Override
public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> chain) {
JavaType jType = Json.mapper().constructType(type);
...
}我写
when(Json.mapper()).thenReturn(objectMapper);
when(objectMapper.constructType(type)).thenReturn(jType);我的错误越来越少
当()需要一个参数时,org.mockito.exceptions.misusing.MissingMethodInvocationException:必须是‘在模拟上调用一个方法’。例如: when(mock.getArticles()).thenReturn(articles); 此外,可能会出现此错误,因为:
发布于 2022-09-16 06:26:47
你是在模仿静态方法。这是不鼓励的,但如果您仍然想这样做,则需要使用Mockito的3.4.0或更高版本的mockStatic法 (应该使用mockito-inline依赖项)。
var objectMapper = mock(ObjectMapper.class);
when(objectMapper.constructType(type)).thenReturn(jType);
try (var mocked = mockStatic(Json.class)) {
mocked.when(Json::mapper).thenReturn(objectMapper);
// execute the tested code
}https://stackoverflow.com/questions/73740464
复制相似问题