英文:
REST assured - compare two JSON objects
问题
假设GET请求返回了一个JSON对象:
{
"a": 1, "b": 2, "c": 3, "date": "current_date"
}
而我手头上有一个类似的对象,我想检查它是否与键"a"、"b"、"c"相同,忽略键"date"。
我该如何做呢?
英文:
Suppose the GET request returns me some JSON object:
{
"a": 1, "b": 2, "c": 3, "date": "current_date"
}
And I have in my hand a similar object, which I'd like to check it's identical for the the keys "a", "b", "c" and ignore the "date" key.
How can I do that?
答案1
得分: 4
我一直在使用JsonUnit,它真的很有帮助
String json1 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"30-07-2020\"\r\n" + "}";
String json2 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"31-07-2020\"\r\n" + "}";
assertThatJson(json1).whenIgnoringPaths("date").isEqualTo(json2);
静态导入:
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
依赖:
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>2.18.1</version>
<scope>test</scope>
</dependency>
你还需要将 json.org
或 Jackson 1.x
或 Jackson 2.x
或 Johnzon
或 Gson
添加到类路径中
我使用Jackson
英文:
I have been using JsonUnit and it really helps
String json1 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"30-07-2020\"\r\n" + "}";
String json2 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"31-07-2020\"\r\n" + "}";
assertThatJson(json1).whenIgnoringPaths("date").isEqualTo(json2);
Static Import :
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
Dependency :
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>2.18.1</version>
<scope>test</scope>
</dependency>
You will have to add json.org
or Jackson 1.x
or Jackson 2.x
or Johnzon
or Gson
to the classpath as well
I use Jackson
答案2
得分: 1
你可以将其转换为Json对象并删除不需要的键。查看以下链接获取详细信息:https://stackoverflow.com/questions/34092346/remove-key-from-a-json-inside-a-jsonobject
英文:
You can transform it to the Json object and delete the unwanted key. Follow the link for details: https://stackoverflow.com/questions/34092346/remove-key-from-a-json-inside-a-jsonobject
答案3
得分: 1
我发现rest-assured有一些有趣的功能。
你可以这样做:
@Test
public void test() {
get("/xxxx").then().statusCode(200).assertThat()
.body("a", equalTo(1));
}
更多信息在这里
英文:
I found out that rest-assured has some interesting functionalities.
You could do:
@Test
public void test() {
get("/xxxx").then().statusCode(200).assertThat()
.body("a", equalTo(1));
}
More info here
答案4
得分: 1
你可以将JSON转换为JS对象,然后比较该对象的每个属性,如果该属性的键不等于"date",则进行比较。
在下面的代码中,将obj1
与obj2
进行比较,并忽略date
属性。
如果它们相同,则打印"identical",如果不同(忽略date
属性),则打印"not identical"。
var obj1 = JSON.parse('{"a":"1","b":"2","c":"3","date":"current_date"}');
var obj2 = JSON.parse('{"a":"1","b":"2","c":"3","date":"another_date"}');
let s1 = Object.keys(obj1).length; // obj1的属性数量
let s2 = Object.keys(obj2).length; // obj2的属性数量
let identical = true;
for (let i = 0; i < s1; i++) {
if (i >= s2) {
identical = false;
break;
}
let current_key = Object.keys(obj1)[i];
let current_value = obj1[current_key];
if (current_key.localeCompare("date") != 0) {
if (current_value.localeCompare(obj2[current_key]) != 0) {
identical = false;
break;
}
}
}
if (identical) {
console.log("Identical");
} else {
console.log("Not identical");
}
英文:
You can transform the JSON into a JS object, then compare each property of that object if that property has a key not equal to "date"
In the code below is comparing obj1
to obj2
and ignoring the date
property.
It prints "identical" if they are both the same and "not identical" if they are not (ignoring the date
property)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var obj1 = JSON.parse ('{ "a":"1","b":"2", "c":"3", "date":"current_date"}' );
var obj2 = JSON.parse ('{ "a":"1","b":"2", "c":"3", "date":"another_date"}' );
let s1 = Object.keys(obj1).length; // length of obj1
let s2 = Object.keys(obj2).length; // length of obj2
let identical = true ;
for ( let i = 0 ; i < s1 ; i ++ ){
if (i >= s2) {
identical = false ;
break ;
}
let current_key = Object.keys(obj1)[i];
let current_value = obj1[current_key];
if (current_key.localeCompare("date") != 0){
if (current_value.localeCompare(obj2[current_key]) != 0){
identical = false ;
break;
}
}
}
if (identical){
console.log ("Identical");
}else {
console.log ("Not identical");
}
<!-- end snippet -->
答案5
得分: 1
-
使用AssertJ忽略比较中的字段
对于需要从响应中反序列化JSON(对于Spring测试:MvcResult.getResponse().getContentAsString()
)为对象,可以使用这种方法。
可以使用REST Assured基于Content-Type的反序列化来反序列化JSON响应。 -
使用JSONassert进行宽松比较。
为了不手动创建JSON,可以使用Jackson的ObjectMapper
来序列化预期对象。对于字段的排除:
Map<String, Object> map = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});
map.remove(...);
objectMapper.writeValueAsString(obj);
英文:
- Use AssertJ ignoring fields in the comparison
For than it's needed to deserialize json from response (for Spring Test:MvcResult.getResponse().getContentAsString()
) to Object.
It is possible to deserialize json response with REST Assured Content-Type based Deserialization - Use unstrict compare with JSONassert.
In order to not creating json by hand, it is possible serialize expected object with JacksonObjectMapper
. For fields exclusion:
Map<String, Object> map = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});
map.remove(...);
objectMapper.writeValueAsString(obj)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论