【问题标题】:How to unit test the JSON exception and hit/cover the catch block如何对 JSON 异常进行单元测试并命中/覆盖 catch 块
【发布时间】:2021-07-26 22:23:45
【问题描述】:

这是我试图涵盖的代码的 sn-p,特别是 catch 块。

JSONObject bodyObject = new JSONObject();
String body = null;

try{
   body = bodyObject.put(DecisionTreeConstants.EDGE_ACCOUNT_STATUS, processStatusPretty).toString();
} catch (JSONException e) {
   LOGGER.debug(" ");
}

【问题讨论】:

  • 你是在测试异常是被抛出还是测试异常的处理?

标签: java json exception junit code-coverage


【解决方案1】:

您需要为注入的 LOGGER 提供一个 testdouble,然后检查是否使用预期的空白字符串调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2013-12-19
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    相关资源
    最近更新 更多