diff --git a/json/pom.xml b/json/pom.xml
index 9b49f4cc7e..87bbad460a 100644
--- a/json/pom.xml
+++ b/json/pom.xml
@@ -63,6 +63,12 @@
${commons-collections4.version}
test
+
+ net.javacrumbs.json-unit
+ json-unit-assertj
+ ${json-unit-assertj.version}
+ test
+
@@ -72,6 +78,7 @@
20211205
2.8.5
1.1.2
+ 2.28.0
\ No newline at end of file
diff --git a/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java
index 441c71e78e..4698b29c6e 100644
--- a/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java
@@ -1,36 +1,44 @@
package com.baeldung.jsonjava;
-import static org.junit.Assert.assertEquals;
-
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONTokener;
import org.junit.Test;
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.assertj.core.api.Assertions.assertThat;
+
public class CDLIntegrationTest {
+
@Test
public void givenCommaDelimitedText_thenConvertToJSONArray() {
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
- assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString());
+
+ assertThatJson(ja)
+ .isEqualTo("[\"England\",\"USA\",\"Canada\"]");
}
@Test
public void givenJSONArray_thenConvertToCommaDelimitedText() {
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
+
String cdt = CDL.rowToString(ja);
- assertEquals("England,USA,Canada", cdt.toString().trim());
+
+ assertThat(cdt.trim()).isEqualTo("England,USA,Canada");
}
@Test
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
- String string =
+ String string =
"name, city, age \n" +
"john, chicago, 22 \n" +
"gary, florida, 35 \n" +
"sal, vegas, 18";
-
+
JSONArray result = CDL.toJSONArray(string);
- assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
+
+ assertThatJson(result)
+ .isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
}
@Test
@@ -39,14 +47,16 @@ public class CDLIntegrationTest {
ja.put("name");
ja.put("city");
ja.put("age");
-
- String string =
+
+ String string =
"john, chicago, 22 \n" +
"gary, florida, 35 \n" +
"sal, vegas, 18";
-
+
JSONArray result = CDL.toJSONArray(ja, string);
- assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
+
+ assertThatJson(result)
+ .isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
}
-
+
}
diff --git a/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java
index 4de5d8a21c..accb94e732 100644
--- a/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java
@@ -1,19 +1,21 @@
package com.baeldung.jsonjava;
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.assertEquals;
import org.json.Cookie;
import org.json.JSONObject;
import org.junit.Test;
public class CookieIntegrationTest {
+
@Test
public void givenCookieString_thenConvertToJSONObject() {
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
JSONObject cookieJO = Cookie.toJSONObject(cookie);
- assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString());
+ assertThatJson(cookieJO)
+ .isEqualTo("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}");
}
@Test
diff --git a/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java
index 1aa0427c7e..ad84c22cca 100644
--- a/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java
@@ -1,10 +1,12 @@
package com.baeldung.jsonjava;
-import static org.junit.Assert.assertEquals;
import org.json.HTTP;
import org.json.JSONObject;
import org.junit.Test;
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.assertj.core.api.Assertions.assertThat;
+
public class HTTPIntegrationTest {
@Test
public void givenJSONObject_thenConvertToHTTPHeader() {
@@ -12,14 +14,16 @@ public class HTTPIntegrationTest {
jo.put("Method", "POST");
jo.put("Request-URI", "http://www.example.com/");
jo.put("HTTP-Version", "HTTP/1.1");
-
- assertEquals("POST \"http://www.example.com/\" HTTP/1.1"+HTTP.CRLF+HTTP.CRLF, HTTP.toString(jo));
+
+ assertThat(HTTP.toString(jo))
+ .isEqualTo("POST \"http://www.example.com/\" HTTP/1.1" + HTTP.CRLF + HTTP.CRLF);
}
@Test
public void givenHTTPHeader_thenConvertToJSONObject() {
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
-
- assertEquals("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}", obj.toString());
+
+ assertThatJson(obj)
+ .isEqualTo("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}");
}
}
diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONArrayGetValueByKeyUnitTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONArrayGetValueByKeyUnitTest.java
index 265c603acd..7ea22b94ba 100644
--- a/json/src/test/java/com/baeldung/jsonjava/JSONArrayGetValueByKeyUnitTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/JSONArrayGetValueByKeyUnitTest.java
@@ -1,12 +1,10 @@
package com.baeldung.jsonjava;
-import java.util.Arrays;
-import java.util.List;
-
import org.junit.Test;
-import static org.junit.Assert.assertThat;
-import static org.hamcrest.CoreMatchers.equalTo;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
public class JSONArrayGetValueByKeyUnitTest {
@@ -19,7 +17,8 @@ public class JSONArrayGetValueByKeyUnitTest {
List actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
- assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
+ assertThat(actualValues)
+ .containsExactlyInAnyOrder("John", "Gary", "Selena");
}
@Test
@@ -29,7 +28,8 @@ public class JSONArrayGetValueByKeyUnitTest {
List actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
- assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
+ assertThat(actualValues)
+ .containsExactlyInAnyOrder("John", "Gary", "Selena");
}
}
diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java
index c956232abe..eed7779e27 100644
--- a/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java
@@ -1,36 +1,40 @@
package com.baeldung.jsonjava;
-import static org.junit.Assert.assertEquals;
-
-import java.util.ArrayList;
-import java.util.List;
-
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
+import java.util.ArrayList;
+import java.util.List;
+
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+
public class JSONArrayIntegrationTest {
+
@Test
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
JSONArray ja = new JSONArray();
ja.put(Boolean.TRUE);
ja.put("lorem ipsum");
-
+
// We can also put a JSONObject in JSONArray
JSONObject jo = new JSONObject();
jo.put("name", "jon doe");
jo.put("age", "22");
jo.put("city", "chicago");
-
+
ja.put(jo);
-
- assertEquals("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]", ja.toString());
+
+ assertThatJson(ja)
+ .isEqualTo("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]");
}
-
+
@Test
public void givenJsonString_thenCreateNewJSONArray() {
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
- assertEquals("[true,\"lorem ipsum\",215]", ja.toString());
+
+ assertThatJson(ja)
+ .isEqualTo("[true,\"lorem ipsum\",215]");
}
@Test
@@ -40,8 +44,10 @@ public class JSONArrayIntegrationTest {
list.add("Texas");
list.add("Hawaii");
list.add("Alaska");
-
+
JSONArray ja = new JSONArray(list);
- assertEquals("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]", ja.toString());
+
+ assertThatJson(ja)
+ .isEqualTo("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]");
}
}
\ No newline at end of file
diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java
index d8571bfcef..4a435a90d3 100644
--- a/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java
@@ -1,23 +1,24 @@
package com.baeldung.jsonjava;
-import static org.junit.Assert.assertEquals;
-
-import java.util.HashMap;
-import java.util.Map;
-
import org.json.JSONObject;
import org.junit.Test;
+import java.util.HashMap;
+import java.util.Map;
+
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+
public class JSONObjectIntegrationTest {
+
@Test
public void givenJSONJava_thenCreateNewJSONObject() {
JSONObject jo = new JSONObject();
jo.put("name", "jon doe");
jo.put("age", "22");
jo.put("city", "chicago");
-
- assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
-
+
+ assertThatJson(jo)
+ .isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
}
@Test
@@ -27,8 +28,9 @@ public class JSONObjectIntegrationTest {
map.put("age", "22");
map.put("city", "chicago");
JSONObject jo = new JSONObject(map);
-
- assertEquals("{\"name\":\"jon doe\",\"city\":\"chicago\",\"age\":\"22\"}", jo.toString());
+
+ assertThatJson(jo)
+ .isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
}
@Test
@@ -36,7 +38,8 @@ public class JSONObjectIntegrationTest {
JSONObject jo = new JSONObject(
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
);
-
- assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
+
+ assertThatJson(jo)
+ .isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
}
}
diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java
index 4fe8f27231..3bd73ca4c2 100644
--- a/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java
@@ -1,21 +1,22 @@
package com.baeldung.jsonjava;
-import static org.junit.Assert.assertEquals;
-
import org.json.JSONTokener;
import org.junit.Test;
+import static org.assertj.core.api.Assertions.assertThat;
+
public class JSONTokenerIntegrationTest {
+
@Test
public void givenString_convertItToJSONTokens() {
String str = "Sample String";
JSONTokener jt = new JSONTokener(str);
-
+
char[] expectedTokens = str.toCharArray();
int index = 0;
-
- while(jt.more()) {
- assertEquals(expectedTokens[index++], jt.next());
+
+ while (jt.more()) {
+ assertThat(jt.next()).isEqualTo(expectedTokens[index++]);
}
}
}
diff --git a/json/src/test/java/com/baeldung/jsonjava/ObjectToFromJSONIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/ObjectToFromJSONIntegrationTest.java
index 99764c5dc9..d1f536d31a 100644
--- a/json/src/test/java/com/baeldung/jsonjava/ObjectToFromJSONIntegrationTest.java
+++ b/json/src/test/java/com/baeldung/jsonjava/ObjectToFromJSONIntegrationTest.java
@@ -1,19 +1,22 @@
package com.baeldung.jsonjava;
-import static org.junit.Assert.assertEquals;
-
import org.json.JSONObject;
import org.junit.Test;
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+
public class ObjectToFromJSONIntegrationTest {
+
@Test
public void givenDemoBean_thenCreateJSONObject() {
DemoBean demo = new DemoBean();
demo.setId(1);
demo.setName("lorem ipsum");
demo.setActive(true);
-
+
JSONObject jo = new JSONObject(demo);
- assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
+
+ assertThatJson(jo)
+ .isEqualTo("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}");
}
}