0

我有以下功能:

public class NorthboundApiLogicTest {

  @Test
  public void testIfParametersValuesAreCorrect() {
    String body = "{\"exception\":\"world\",\"error\":\"Not Found\",\"message\":\"hello\""
        + ",\"timestamp\":\"2020-01-07T12:26:48.334386Z\",\"status\":\"404\"}";
    try {
      JSONAssert.assertEquals("{message:hello, exception:world, status:403, error:Not Found}", 
          body, false);
    } catch (Exception e) {
      System.err.println(e);
    }
    System.out.println(body);
  }
}

我用 Maven 运行这个测试,奇怪的是它成功通过了。

在此处输入图像描述

但是,它不应该,因为我断言status=403但值是404. 我究竟做错了什么?

4

1 回答 1

4

它甚至在对结构执行断言之前无法解析JSON,而您只是通过catch.

catch (Exception e) {
      System.err.println(e);
}

您需要让该异常从方法中抛出,或者捕获它并fail()使用适当的消息进行调用。

然后解决您的不可解析 JSON 的问题,当然!

于 2020-01-07T12:41:19.160 回答