【发布时间】:2020-02-13 20:10:45
【问题描述】:
我在一个名为“HttpResponseHelper”的类中有一个方法,当它抛出 JsonProcessingException 时,我试图对其进行单元测试,但我很难做到这一点:
private static void populateHTTPResponseWithData(ObjectNode httpResponse)
{
ObjectMapper mapper = new ObjectMapper();
responseMapData.keySet().forEach(item -> {
try
{
httpResponse.put(item, mapper.writeValueAsString(responseMapData.get(item)));
}
catch (JsonProcessingException e)
{
LOGGER.error("Json Processing Exception", e);
}
});
}
httpResponse 参数是 ObjectNode 类型(Jackson 库),然后在方法主体内从 ObjectMapper 类创建一个映射器对象。
resonseMapData 是来自名为“MessageProcessResults”的类的 ConcurrentHashMap>。它看起来像这里它循环通过 keySet 并在 httpResponse 参数内插入键值对的字符串。
我尝试在 mapper 上使用 mockito 来返回格式错误的 JSON,但它看起来像将值写入 String 并且每次都通过。
有没有人有任何建议或者有一个简单的方法可以做到这一点?感谢您花时间阅读这个问题并可能帮助我:D
【问题讨论】:
-
这个方法从不抛出
JsonProcessingException,它会捕获它并只记录结果,如果你将throw e放在你的catch块中它也会抛出它,但它不会继续处理发生异常时,responseMapData中的其他项目。 -
我不知道你为什么需要测试它;我假设只是为了获得 100% 的 catch 块覆盖率,但您可能不会测试日志记录是否发生。最好不要担心它。但是您应该注入 ObjectMapper 而不是创建一个,然后您可以在测试中注入一个模拟映射器,并在调用 writeValueAsString 时将其抛出。
标签: java unit-testing jackson jsonexception