我有以下功能:
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
。我究竟做错了什么?
最佳答案
在对结构执行断言之前,它无法解析 JSON,而您只需通过 catch
进行记录即可。
catch (Exception e) {
System.err.println(e);
}
您需要让该异常从方法中抛出,或者捕获它并使用适当的消息调用 fail()
。
当然,然后解决不可解析的 JSON 问题!
关于Java JSON 单元测试通过但值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59628559/