Skip to content

Commit 3ede690

Browse files
committed
[JAVA-9317] Update tests to use JsonUnit for improved assertions
1 parent c9a9a70 commit 3ede690

9 files changed

+94
-58
lines changed

Diff for: json/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
<version>${commons-collections4.version}</version>
6464
<scope>test</scope>
6565
</dependency>
66+
<dependency>
67+
<groupId>net.javacrumbs.json-unit</groupId>
68+
<artifactId>json-unit-assertj</artifactId>
69+
<version>${json-unit-assertj.version}</version>
70+
<scope>test</scope>
71+
</dependency>
6672
</dependencies>
6773

6874
<properties>
@@ -72,6 +78,7 @@
7278
<json.version>20211205</json.version>
7379
<gson.version>2.8.5</gson.version>
7480
<javax.version>1.1.2</javax.version>
81+
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
7582
</properties>
7683

7784
</project>
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
package com.baeldung.jsonjava;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
import org.json.CDL;
64
import org.json.JSONArray;
75
import org.json.JSONTokener;
86
import org.junit.Test;
97

8+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
1011
public class CDLIntegrationTest {
12+
1113
@Test
1214
public void givenCommaDelimitedText_thenConvertToJSONArray() {
1315
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
14-
assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString());
16+
17+
assertThatJson(ja)
18+
.isEqualTo("[\"England\",\"USA\",\"Canada\"]");
1519
}
1620

1721
@Test
1822
public void givenJSONArray_thenConvertToCommaDelimitedText() {
1923
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
24+
2025
String cdt = CDL.rowToString(ja);
21-
assertEquals("England,USA,Canada", cdt.toString().trim());
26+
27+
assertThat(cdt.trim()).isEqualTo("England,USA,Canada");
2228
}
2329

2430
@Test
2531
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
26-
String string =
32+
String string =
2733
"name, city, age \n" +
2834
"john, chicago, 22 \n" +
2935
"gary, florida, 35 \n" +
3036
"sal, vegas, 18";
31-
37+
3238
JSONArray result = CDL.toJSONArray(string);
33-
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
39+
40+
assertThatJson(result)
41+
.isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
3442
}
3543

3644
@Test
@@ -39,14 +47,16 @@ public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects2() {
3947
ja.put("name");
4048
ja.put("city");
4149
ja.put("age");
42-
43-
String string =
50+
51+
String string =
4452
"john, chicago, 22 \n" +
4553
"gary, florida, 35 \n" +
4654
"sal, vegas, 18";
47-
55+
4856
JSONArray result = CDL.toJSONArray(ja, string);
49-
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
57+
58+
assertThatJson(result)
59+
.isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
5060
}
51-
61+
5262
}

Diff for: json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package com.baeldung.jsonjava;
22

3+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
34
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.junit.Assert.assertEquals;
55

66
import org.json.Cookie;
77
import org.json.JSONObject;
88
import org.junit.Test;
99

1010
public class CookieIntegrationTest {
11+
1112
@Test
1213
public void givenCookieString_thenConvertToJSONObject() {
1314
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
1415
JSONObject cookieJO = Cookie.toJSONObject(cookie);
1516

16-
assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString());
17+
assertThatJson(cookieJO)
18+
.isEqualTo("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}");
1719
}
1820

1921
@Test
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package com.baeldung.jsonjava;
22

3-
import static org.junit.Assert.assertEquals;
43
import org.json.HTTP;
54
import org.json.JSONObject;
65
import org.junit.Test;
76

7+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
810
public class HTTPIntegrationTest {
911
@Test
1012
public void givenJSONObject_thenConvertToHTTPHeader() {
1113
JSONObject jo = new JSONObject();
1214
jo.put("Method", "POST");
1315
jo.put("Request-URI", "http://www.example.com/");
1416
jo.put("HTTP-Version", "HTTP/1.1");
15-
16-
assertEquals("POST \"http://www.example.com/\" HTTP/1.1"+HTTP.CRLF+HTTP.CRLF, HTTP.toString(jo));
17+
18+
assertThat(HTTP.toString(jo))
19+
.isEqualTo("POST \"http://www.example.com/\" HTTP/1.1" + HTTP.CRLF + HTTP.CRLF);
1720
}
1821

1922
@Test
2023
public void givenHTTPHeader_thenConvertToJSONObject() {
2124
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
22-
23-
assertEquals("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}", obj.toString());
25+
26+
assertThatJson(obj)
27+
.isEqualTo("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}");
2428
}
2529
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.baeldung.jsonjava;
22

3-
import java.util.Arrays;
4-
import java.util.List;
5-
63
import org.junit.Test;
74

8-
import static org.junit.Assert.assertThat;
9-
import static org.hamcrest.CoreMatchers.equalTo;
5+
import java.util.List;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
108

119
public class JSONArrayGetValueByKeyUnitTest {
1210

@@ -19,7 +17,8 @@ public void givenJSONArrayAndAKey_thenReturnAllValuesForGivenKey() {
1917

2018
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
2119

22-
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
20+
assertThat(actualValues)
21+
.containsExactlyInAnyOrder("John", "Gary", "Selena");
2322
}
2423

2524
@Test
@@ -29,7 +28,8 @@ public void givenJSONArrayAndAKey_whenUsingJava8Syntax_thenReturnAllValuesForGiv
2928

3029
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
3130

32-
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
31+
assertThat(actualValues)
32+
.containsExactlyInAnyOrder("John", "Gary", "Selena");
3333
}
3434

3535
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
package com.baeldung.jsonjava;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
import org.junit.Test;
46

57
import java.util.ArrayList;
68
import java.util.List;
79

8-
import org.json.JSONArray;
9-
import org.json.JSONObject;
10-
import org.junit.Test;
10+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
1111

1212
public class JSONArrayIntegrationTest {
13+
1314
@Test
1415
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
1516
JSONArray ja = new JSONArray();
1617
ja.put(Boolean.TRUE);
1718
ja.put("lorem ipsum");
18-
19+
1920
// We can also put a JSONObject in JSONArray
2021
JSONObject jo = new JSONObject();
2122
jo.put("name", "jon doe");
2223
jo.put("age", "22");
2324
jo.put("city", "chicago");
24-
25+
2526
ja.put(jo);
26-
27-
assertEquals("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]", ja.toString());
27+
28+
assertThatJson(ja)
29+
.isEqualTo("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]");
2830
}
29-
31+
3032
@Test
3133
public void givenJsonString_thenCreateNewJSONArray() {
3234
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
33-
assertEquals("[true,\"lorem ipsum\",215]", ja.toString());
35+
36+
assertThatJson(ja)
37+
.isEqualTo("[true,\"lorem ipsum\",215]");
3438
}
3539

3640
@Test
@@ -40,8 +44,10 @@ public void givenListObject_thenConvertItToJSONArray() {
4044
list.add("Texas");
4145
list.add("Hawaii");
4246
list.add("Alaska");
43-
47+
4448
JSONArray ja = new JSONArray(list);
45-
assertEquals("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]", ja.toString());
49+
50+
assertThatJson(ja)
51+
.isEqualTo("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]");
4652
}
4753
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package com.baeldung.jsonjava;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.json.JSONObject;
4+
import org.junit.Test;
45

56
import java.util.HashMap;
67
import java.util.Map;
78

8-
import org.json.JSONObject;
9-
import org.junit.Test;
9+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
1010

1111
public class JSONObjectIntegrationTest {
12+
1213
@Test
1314
public void givenJSONJava_thenCreateNewJSONObject() {
1415
JSONObject jo = new JSONObject();
1516
jo.put("name", "jon doe");
1617
jo.put("age", "22");
1718
jo.put("city", "chicago");
18-
19-
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
20-
19+
20+
assertThatJson(jo)
21+
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
2122
}
2223

2324
@Test
@@ -27,16 +28,18 @@ public void givenMapObject_thenCreateJSONObject() {
2728
map.put("age", "22");
2829
map.put("city", "chicago");
2930
JSONObject jo = new JSONObject(map);
30-
31-
assertEquals("{\"name\":\"jon doe\",\"city\":\"chicago\",\"age\":\"22\"}", jo.toString());
31+
32+
assertThatJson(jo)
33+
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
3234
}
3335

3436
@Test
3537
public void givenJsonString_thenCreateJSONObject() {
3638
JSONObject jo = new JSONObject(
3739
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
3840
);
39-
40-
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
41+
42+
assertThatJson(jo)
43+
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
4144
}
4245
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package com.baeldung.jsonjava;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
import org.json.JSONTokener;
64
import org.junit.Test;
75

6+
import static org.assertj.core.api.Assertions.assertThat;
7+
88
public class JSONTokenerIntegrationTest {
9+
910
@Test
1011
public void givenString_convertItToJSONTokens() {
1112
String str = "Sample String";
1213
JSONTokener jt = new JSONTokener(str);
13-
14+
1415
char[] expectedTokens = str.toCharArray();
1516
int index = 0;
16-
17-
while(jt.more()) {
18-
assertEquals(expectedTokens[index++], jt.next());
17+
18+
while (jt.more()) {
19+
assertThat(jt.next()).isEqualTo(expectedTokens[index++]);
1920
}
2021
}
2122
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package com.baeldung.jsonjava;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
import org.json.JSONObject;
64
import org.junit.Test;
75

6+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
7+
88
public class ObjectToFromJSONIntegrationTest {
9+
910
@Test
1011
public void givenDemoBean_thenCreateJSONObject() {
1112
DemoBean demo = new DemoBean();
1213
demo.setId(1);
1314
demo.setName("lorem ipsum");
1415
demo.setActive(true);
15-
16+
1617
JSONObject jo = new JSONObject(demo);
17-
assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
18+
19+
assertThatJson(jo)
20+
.isEqualTo("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}");
1821
}
1922
}

0 commit comments

Comments
 (0)