When automating the interface, we need to verify the content returned by the interface. The result usually returned by Rest Api is Json. If you only verify the success or failure or individual fields, will you rest assured? Ideally, we want to return the Json check for the whole. It is impossible for us to write an Assert statement for each field. If you compare Json and Json directly, the test case will be unstable because the order of nodes or individual fields change every time. Many students will choose to develop a class library for Json comparison to ignore the order of nodes or the value of a certain field. Instead of working behind closed doors, stand on the shoulders of giants. Occasionally I found JsonUnit on Github, which is exactly what I wanted.
rely
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
It is not necessary to introduce gson, either gson or jackson will do. If none of them are introduced, an error will be reported.
Small scale chopper
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
assertThatJson("{\"a\":1, \"b\":2}").as("两个Json模糊比较")
.isEqualTo("{b:2, a:1}");
JsonUnit supports AssertJ and Hamcrest, my example here is AssertJ.
Ignore path
assertThatJson("{\"root\":{\"test\":1, \"ignored\": 1}}")
.whenIgnoringPaths("root.ignored"))
.isEqualTo("{\"root\":{\"test\":1}}");
What if you want to ignore multipath? Use, separate
Ignore value
assertThatJson("{\"a\":1}")
.isEqualTo(json("{\"a\":\"${json-unit.ignore}\"}"));
Ignore elements
assertThatJson("{\"a\":1}")
.isEqualTo(json("{\"a\":\"${json-unit.ignore}\"}"));
JsonPath
assertThatJson(response.asString())
.inPath("$.data.list")
.isArray()
.contains(json(expMap.get("xx")
.getExpJson()));
Type placeholder
assertThatJson("{\"test\":\"value\"}")
.isEqualTo("{test:'${json-unit.any-string}'}");
assertThatJson("{\"test\":true}")
.isEqualTo("{\"test\":\"${json-unit.any-boolean}\"}");
assertThatJson("{\"test\":1.1}")
.isEqualTo("{\"test\":\"${json-unit.any-number}\"}");
Regular expression
assertJsonEquals("{\"test\": \"${json-unit.regex}[A-Z]+\"}",
"{\"test\": \"ABCD\"}");
options
TREATING_NULL_AS_ABSENT - 把Null当作不存在
assertJsonEquals("{\"test\":{\"a\":1}}",
"{\"test\":{\"a\":1, \"b\": null, \"c\": null}}",
when(TREATING_NULL_AS_ABSENT));
IGNORING_ARRAY_ORDER - 忽略数组元素顺序
assertJsonEquals("{\"test\":[1,2,3]}",
"{\"test\":[3,2,1]}",
when(IGNORING_ARRAY_ORDER));
IGNORING_EXTRA_ARRAY_ITEMS - 忽略多余的元素
assertJsonEquals("{\"test\":[1,2,3]}",
"{\"test\":[1,2,3,4]}",
when(IGNORING_EXTRA_ARRAY_ITEMS));
assertJsonEquals("{\"test\":[1,2,3]}",
"{\"test\":[5,5,4,4,3,3,2,2,1,1]}",
when(IGNORING_EXTRA_ARRAY_ITEMS, IGNORING_ARRAY_ORDER));
IGNORING_EXTRA_FIELDS - 忽略多余字段
assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}")
.when(IGNORING_EXTRA_FIELDS)
.isEqualTo("{\"test\":{\"b\":2}}");
IGNORE_VALUES - 忽略值
assertJsonEquals("{\"test\":{\"a\":1,\"b\":2,\"c\":3}}",
"{\"test\":{\"a\":3,\"b\":2,\"c\":1}}",
when(IGNORING_VALUES));
Conclusion
JsonUnit is very powerful, and only departmental features are used in the project. In the real project, Json will be stored in the file. Then read them together in beforeMethod.
I am a test lady in the workplace! Just finished the test tutorial, I will share it again. Testers who are interested in python automated testing, web automation, interface automation, mobile terminal automation, interview experience exchange, etc., can follow the WeChat public account:[Sad Spicy Strips], Obtain the interview information of the software test engineer from Dachang! My learning exchange group: 902061117 group has technical experts to communicate and share together~
If the article is helpful to you, please reach out to make a fortune and give me a like. Thank you for your support. Your likes are my motivation for continuous updating.
Recommended reading:
What kind of person is suitable for software testing?
Talking about starting from a small company to a big factory, what did I do right?
Want to switch to software testing? Come and see if you are suitable
From self-study to work in software testing, how should software testing learning be carried out?