【发布时间】:2020-06-20 00:08:56
【问题描述】:
我有一个本地 json 文件 test.json
[
{
"id": 1,
"title": "test1"
},
{
"id": 2,
"title": "test2"
}
]
读取json文件的类
public static String getFileContent(String fileName){
String fileContent = "";
String filePath = "filePath";
try {
fileContent = new String(Files.readAllBytes(Paths.get(filePath)));
return fileContent;
}catch(Exception ex){
ex.printStackTrace();
}finally{
return fileContent;
}
}
我使用放心发出请求并得到相同的 json 响应
String fileContent= FileUtils.getFileContent("test.json");
when().
get("/testurl").
then().
body("", equalTo(fileContent));
这是我从本地文件中得到的
[\r\n {\r\n \"id\": 1,\r\n \"title\": \"test1\"\r\n },\r\n {\r\n \"id\": 2,\r\n \"title\": \"test2\"\r\n }\r\n]
这是实际的响应:
[{id=1, title=test1}, {id=2, title=test2}]
有没有更好的方法来比较这两者?我尝试做类似fileContent.replaceAll("\\r\\n| |\"", ""); 的事情,但它只是删除了所有空间[{id:1,title:test1},{id:2,title:test2}]
有什么帮助吗?或者任何只比较内容而忽略换行符、空格和双引号的方法?
【问题讨论】:
-
这能回答你的问题吗? Full Json match with RestAssured
-
参考使用 Hamcrest Matchers 的@Hac 给出的答案。
-
@kaweesha - 我在发布答案之前检查了该帖子,
expectedJson.getMap("")在 JSON 作为 ArrayList 时不起作用,这里的工作解决方案是expectedJson.getList("")
标签: json string-comparison rest-assured