gpt4 book ai didi

Java JSON 单元测试通过但值错误

转载 作者:行者123 更新时间:2023-12-01 17:42:18 26 4
gpt4 key购买 nike

我有以下功能:

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 运行这个测试,奇怪的是它成功通过了。

enter image description here

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

最佳答案

在对结构执行断言之前,它无法解析 JSON,而您只需通过 catch 进行记录即可。

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

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

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

关于Java JSON 单元测试通过但值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59628559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com