Posted In: JSON
Check two JSON Objects for equality – Hamcrest-JSON
This example uses hamcrest-json library
<dependency> <groupId>uk.co.datumedge</groupId> <artifactId>hamcrest-json</artifactId> <version>0.2</version> </dependency>
Examples using Hamcrest-JSON
1. Success test with a org.json.JSONObject
@Test public void testJson1() { assertThat(expectedJsonObject1(), SameJSONAs.sameJSONObjectAs(actualJsonObject1()) .allowingExtraUnexpectedFields() .allowingAnyArrayOrdering()); } private JSONObject expectedJsonObject1() { JSONObject expected = new JSONObject(); expected.put("ID", "40"); return expected; } private JSONObject actualJsonObject1() { JSONObject actual = new JSONObject(); actual.put("ID", "40"); return actual; }
2. Error test with a org.json.JSONObject
@Test public void testJson2() { assertThat(expectedJsonObject2(), SameJSONAs.sameJSONObjectAs(actualJsonObject2()) .allowingExtraUnexpectedFields() .allowingAnyArrayOrdering()); } private JSONObject expectedJsonObject2() { JSONObject expected = new JSONObject(); expected.put("ID", "40"); return expected; } private JSONObject actualJsonObject2() { JSONObject actual = new JSONObject(); actual.put("ID", "41"); return actual; }
3. Success test with a JSON String
@Test public void testJson3() { assertThat(expectedData3(), SameJSONAs.sameJSONAs(actualData3()) .allowingExtraUnexpectedFields() .allowingAnyArrayOrdering()); } private String expectedData3() { String json = "{" + " \"portfolio\": [" + " {" + " \"stock\": \"Infosys\"," + " \"quantity\": \"100\"," + " \"buy\": \"1011\"," + " \"profit\": \"10000\"" + " }," + " {" + " \"stock\": \"State Bank of India\"," + " \"quantity\": \"1000\"," + " \"buy\": \"204\"," + " \"profit\": \"20000\"" + " }," + " {" + " \"stock\": \"Reliance Industries\"," + " \"quantity\": \"100\"," + " \"buy\": \"1200\"," + " \"profit\": \"12000\"" + " }" + " ]" + "}"; return json; } private String actualData3() { String json = "{" + " \"portfolio\": [" + " {" + " \"stock\": \"Infosys\"," + " \"quantity\": \"100\"," + " \"buy\": \"1011\"," + " \"profit\": \"10000\"" + " }," + " {" + " \"stock\": \"State Bank of India\"," + " \"quantity\": \"1000\"," + " \"buy\": \"204\"," + " \"profit\": \"20000\"" + " }," + " {" + " \"stock\": \"Reliance Industries\"," + " \"quantity\": \"100\"," + " \"buy\": \"1200\"," + " \"profit\": \"12000\"" + " }" + " ]" + "}"; return json; }
4. Error test with a JSON String
@Test public void testJson4() { assertThat(expectedData4(), SameJSONAs.sameJSONAs(actualData4()) .allowingExtraUnexpectedFields() .allowingAnyArrayOrdering()); } private String expectedData4() { String json = "{" + " \"portfolio\": [" + " {" + " \"stock\": \"Infosys\"," + " \"quantity\": \"100\"," + " \"buy\": \"1011\"," + " \"profit\": \"10000\"" + " }," + " {" + " \"stock\": \"State Bank of India\"," + " \"quantity\": \"1000\"," + " \"buy\": \"204\"," + " \"profit\": \"20000\"" + " }," + " {" + " \"stock\": \"Reliance Industries\"," + " \"quantity\": \"100\"," + " \"buy\": \"1200\"," + " \"profit\": \"12000\"" + " }" + " ]" + "}"; return json; } private String actualData4() { String json = "{" + " \"portfolio\": [" + " {" + " \"stock\": \"Infosys1\"," + " \"quantity\": \"100\"," + " \"buy\": \"1011\"," + " \"profit\": \"10000\"" + " }," + " {" + " \"stock\": \"State Bank of India\"," + " \"quantity\": \"1000\"," + " \"buy\": \"204\"," + " \"profit\": \"200000\"" + " }," + " {" + " \"stock\": \"Reliance Industries\"," + " \"quantity\": \"1000\"," + " \"buy\": \"1200\"," + " \"profit\": \"12000\"" + " }" + " ]" + "}"; return json; }
Tags: Compare two JSON objects in Java, Hamcrest, JSON, JSONAssert
- Apache (13)
- Build Tools (2)
- Gradle (2)
- Caching (1)
- cpanel (1)
- cURL (1)
- Database (7)
- Hibernate (5)
- Java Core (38)
- Java Script (15)
- Bootstrap (1)
- File Upload (7)
- jQuery (3)
- React (3)
- JEE (13)
- JSON (41)
- GSON (13)
- Jackson 1X (1)
- Jackson 2X (12)
- jsoniter (1)
- Logging (2)
- Apache Commons Logging (1)
- Apache Log4J (1)
- Logback (1)
- SLF4J (1)
- MongoDB (1)
- OS (1)
- Linux (1)
- Security (5)
- Server (4)
- Tomcat (4)
- Service (2)
- Micro (2)
- Spring (46)
- Pattern (2)
- Spring Boot (20)
- Spring Data (4)
- Spring MVC (8)
- Spring REST (13)
- Spring Security (7)
- Testing (11)
- XML (5)
- JDOM XML Parser (1)