[JAVA-9317] Update tests to use JsonUnit for improved assertions
This commit is contained in:
parent
ff081330bc
commit
c86e2a8655
|
@ -63,6 +63,12 @@
|
||||||
<version>${commons-collections4.version}</version>
|
<version>${commons-collections4.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.javacrumbs.json-unit</groupId>
|
||||||
|
<artifactId>json-unit-assertj</artifactId>
|
||||||
|
<version>${json-unit-assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -72,6 +78,7 @@
|
||||||
<json.version>20211205</json.version>
|
<json.version>20211205</json.version>
|
||||||
<gson.version>2.8.5</gson.version>
|
<gson.version>2.8.5</gson.version>
|
||||||
<javax.version>1.1.2</javax.version>
|
<javax.version>1.1.2</javax.version>
|
||||||
|
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,24 +1,30 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.CDL;
|
import org.json.CDL;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONTokener;
|
import org.json.JSONTokener;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class CDLIntegrationTest {
|
public class CDLIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCommaDelimitedText_thenConvertToJSONArray() {
|
public void givenCommaDelimitedText_thenConvertToJSONArray() {
|
||||||
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
|
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
|
||||||
assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString());
|
|
||||||
|
assertThatJson(ja)
|
||||||
|
.isEqualTo("[\"England\",\"USA\",\"Canada\"]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONArray_thenConvertToCommaDelimitedText() {
|
public void givenJSONArray_thenConvertToCommaDelimitedText() {
|
||||||
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
|
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
|
||||||
|
|
||||||
String cdt = CDL.rowToString(ja);
|
String cdt = CDL.rowToString(ja);
|
||||||
assertEquals("England,USA,Canada", cdt.toString().trim());
|
|
||||||
|
assertThat(cdt.trim()).isEqualTo("England,USA,Canada");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -30,7 +36,9 @@ public class CDLIntegrationTest {
|
||||||
"sal, vegas, 18";
|
"sal, vegas, 18";
|
||||||
|
|
||||||
JSONArray result = CDL.toJSONArray(string);
|
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
|
@Test
|
||||||
|
@ -46,7 +54,9 @@ public class CDLIntegrationTest {
|
||||||
"sal, vegas, 18";
|
"sal, vegas, 18";
|
||||||
|
|
||||||
JSONArray result = CDL.toJSONArray(ja, string);
|
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\"}]");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.Cookie;
|
import org.json.Cookie;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class CookieIntegrationTest {
|
public class CookieIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCookieString_thenConvertToJSONObject() {
|
public void givenCookieString_thenConvertToJSONObject() {
|
||||||
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
|
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
|
||||||
JSONObject cookieJO = Cookie.toJSONObject(cookie);
|
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
|
@Test
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import org.json.HTTP;
|
import org.json.HTTP;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class HTTPIntegrationTest {
|
public class HTTPIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONObject_thenConvertToHTTPHeader() {
|
public void givenJSONObject_thenConvertToHTTPHeader() {
|
||||||
|
@ -13,13 +15,15 @@ public class HTTPIntegrationTest {
|
||||||
jo.put("Request-URI", "http://www.example.com/");
|
jo.put("Request-URI", "http://www.example.com/");
|
||||||
jo.put("HTTP-Version", "HTTP/1.1");
|
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
|
@Test
|
||||||
public void givenHTTPHeader_thenConvertToJSONObject() {
|
public void givenHTTPHeader_thenConvertToJSONObject() {
|
||||||
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
|
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\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertThat;
|
import java.util.List;
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class JSONArrayGetValueByKeyUnitTest {
|
public class JSONArrayGetValueByKeyUnitTest {
|
||||||
|
|
||||||
|
@ -19,7 +17,8 @@ public class JSONArrayGetValueByKeyUnitTest {
|
||||||
|
|
||||||
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
|
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
|
||||||
|
|
||||||
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
|
assertThat(actualValues)
|
||||||
|
.containsExactlyInAnyOrder("John", "Gary", "Selena");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -29,7 +28,8 @@ public class JSONArrayGetValueByKeyUnitTest {
|
||||||
|
|
||||||
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
|
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
|
||||||
|
|
||||||
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
|
assertThat(actualValues)
|
||||||
|
.containsExactlyInAnyOrder("John", "Gary", "Selena");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
package com.baeldung.jsonjava;
|
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.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
|
||||||
public class JSONArrayIntegrationTest {
|
public class JSONArrayIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
|
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
|
||||||
JSONArray ja = new JSONArray();
|
JSONArray ja = new JSONArray();
|
||||||
|
@ -24,13 +25,16 @@ public class JSONArrayIntegrationTest {
|
||||||
|
|
||||||
ja.put(jo);
|
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
|
@Test
|
||||||
public void givenJsonString_thenCreateNewJSONArray() {
|
public void givenJsonString_thenCreateNewJSONArray() {
|
||||||
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
|
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
|
||||||
assertEquals("[true,\"lorem ipsum\",215]", ja.toString());
|
|
||||||
|
assertThatJson(ja)
|
||||||
|
.isEqualTo("[true,\"lorem ipsum\",215]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -42,6 +46,8 @@ public class JSONArrayIntegrationTest {
|
||||||
list.add("Alaska");
|
list.add("Alaska");
|
||||||
|
|
||||||
JSONArray ja = new JSONArray(list);
|
JSONArray ja = new JSONArray(list);
|
||||||
assertEquals("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]", ja.toString());
|
|
||||||
|
assertThatJson(ja)
|
||||||
|
.isEqualTo("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,14 +1,15 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
|
||||||
public class JSONObjectIntegrationTest {
|
public class JSONObjectIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONJava_thenCreateNewJSONObject() {
|
public void givenJSONJava_thenCreateNewJSONObject() {
|
||||||
JSONObject jo = new JSONObject();
|
JSONObject jo = new JSONObject();
|
||||||
|
@ -16,8 +17,8 @@ public class JSONObjectIntegrationTest {
|
||||||
jo.put("age", "22");
|
jo.put("age", "22");
|
||||||
jo.put("city", "chicago");
|
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
|
@Test
|
||||||
|
@ -28,7 +29,8 @@ public class JSONObjectIntegrationTest {
|
||||||
map.put("city", "chicago");
|
map.put("city", "chicago");
|
||||||
JSONObject jo = new JSONObject(map);
|
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
|
@Test
|
||||||
|
@ -37,6 +39,7 @@ public class JSONObjectIntegrationTest {
|
||||||
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
|
"{\"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\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.JSONTokener;
|
import org.json.JSONTokener;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class JSONTokenerIntegrationTest {
|
public class JSONTokenerIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_convertItToJSONTokens() {
|
public void givenString_convertItToJSONTokens() {
|
||||||
String str = "Sample String";
|
String str = "Sample String";
|
||||||
|
@ -15,7 +16,7 @@ public class JSONTokenerIntegrationTest {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
while (jt.more()) {
|
while (jt.more()) {
|
||||||
assertEquals(expectedTokens[index++], jt.next());
|
assertThat(jt.next()).isEqualTo(expectedTokens[index++]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
|
||||||
public class ObjectToFromJSONIntegrationTest {
|
public class ObjectToFromJSONIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDemoBean_thenCreateJSONObject() {
|
public void givenDemoBean_thenCreateJSONObject() {
|
||||||
DemoBean demo = new DemoBean();
|
DemoBean demo = new DemoBean();
|
||||||
|
@ -14,6 +15,8 @@ public class ObjectToFromJSONIntegrationTest {
|
||||||
demo.setActive(true);
|
demo.setActive(true);
|
||||||
|
|
||||||
JSONObject jo = new JSONObject(demo);
|
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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue