REST assured – 比较两个JSON对象

huangapple go评论57阅读模式
英文:

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.orgJackson 1.xJackson 2.xJohnzonGson 添加到类路径中

我使用Jackson

英文:

I have been using JsonUnit and it really helps

String json1 = &quot;{\r\n&quot; + &quot;	\&quot;a\&quot;: 1,\r\n&quot; + &quot;	\&quot;b\&quot;: 2,\r\n&quot; + &quot;	\&quot;c\&quot;: 3,\r\n&quot;
			+ &quot;	\&quot;date\&quot;: \&quot;30-07-2020\&quot;\r\n&quot; + &quot;}&quot;;

String json2 = &quot;{\r\n&quot; + &quot;	\&quot;a\&quot;: 1,\r\n&quot; + &quot;	\&quot;b\&quot;: 2,\r\n&quot; + &quot;	\&quot;c\&quot;: 3,\r\n&quot;
			+ &quot;	\&quot;date\&quot;: \&quot;31-07-2020\&quot;\r\n&quot; + &quot;}&quot;;

assertThatJson(json1).whenIgnoringPaths(&quot;date&quot;).isEqualTo(json2);

Static Import :

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;

Dependency :

	&lt;dependency&gt;
		&lt;groupId&gt;net.javacrumbs.json-unit&lt;/groupId&gt;
		&lt;artifactId&gt;json-unit-assertj&lt;/artifactId&gt;
		&lt;version&gt;2.18.1&lt;/version&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;

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(&quot;/xxxx&quot;).then().statusCode(200).assertThat()
  .body(&quot;a&quot;, equalTo(1)); 
}

More info here

答案4

得分: 1

你可以将JSON转换为JS对象,然后比较该对象的每个属性,如果该属性的键不等于"date",则进行比较。

在下面的代码中,将obj1obj2进行比较,并忽略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 (&#39;{ &quot;a&quot;:&quot;1&quot;,&quot;b&quot;:&quot;2&quot;, &quot;c&quot;:&quot;3&quot;, &quot;date&quot;:&quot;current_date&quot;}&#39; );

var obj2 = JSON.parse (&#39;{ &quot;a&quot;:&quot;1&quot;,&quot;b&quot;:&quot;2&quot;, &quot;c&quot;:&quot;3&quot;, &quot;date&quot;:&quot;another_date&quot;}&#39; );

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 &lt; s1 ; i ++ ){
  if (i &gt;= s2) {
    identical = false ;
    break ;
  }
  
  let current_key = Object.keys(obj1)[i];
  let current_value = obj1[current_key];
  
  if (current_key.localeCompare(&quot;date&quot;) != 0){
     if (current_value.localeCompare(obj2[current_key]) != 0){
       identical = false ;
       break;
     }
  }
 
}

if (identical){
  console.log (&quot;Identical&quot;);
}else {
  console.log (&quot;Not identical&quot;);
}

<!-- end snippet -->

答案5

得分: 1

  1. 使用AssertJ忽略比较中的字段
    对于需要从响应中反序列化JSON(对于Spring测试:MvcResult.getResponse().getContentAsString())为对象,可以使用这种方法。
    可以使用REST Assured基于Content-Type的反序列化来反序列化JSON响应。

  2. 使用JSONassert进行宽松比较。
    为了不手动创建JSON,可以使用Jackson的ObjectMapper来序列化预期对象。对于字段的排除:

Map<String, Object> map = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});
map.remove(...);
objectMapper.writeValueAsString(obj);
英文:
  1. 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
  2. Use unstrict compare with JSONassert.
    In order to not creating json by hand, it is possible serialize expected object with Jackson ObjectMapper. For fields exclusion:
Map&lt;String, Object&gt; map = objectMapper.convertValue(obj, new TypeReference&lt;Map&lt;String, Object&gt;&gt;() {});
map.remove(...);
objectMapper.writeValueAsString(obj)


</details>



huangapple
  • 本文由 发表于 2020年7月30日 21:58:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63174787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定