[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>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.javacrumbs.json-unit</groupId>
|
||||
<artifactId>json-unit-assertj</artifactId>
|
||||
<version>${json-unit-assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -72,6 +78,7 @@
|
|||
<json.version>20211205</json.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<javax.version>1.1.2</javax.version>
|
||||
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,24 +1,30 @@
|
|||
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
|
||||
|
@ -30,7 +36,9 @@ public class CDLIntegrationTest {
|
|||
"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
|
||||
|
@ -46,7 +54,9 @@ public class CDLIntegrationTest {
|
|||
"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\"}]");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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() {
|
||||
|
@ -13,13 +15,15 @@ public class HTTPIntegrationTest {
|
|||
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\"}");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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<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;
|
||||
|
||||
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();
|
||||
|
@ -24,13 +25,16 @@ public class JSONArrayIntegrationTest {
|
|||
|
||||
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
|
||||
|
@ -42,6 +46,8 @@ public class JSONArrayIntegrationTest {
|
|||
list.add("Alaska");
|
||||
|
||||
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;
|
||||
|
||||
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();
|
||||
|
@ -16,8 +17,8 @@ public class JSONObjectIntegrationTest {
|
|||
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
|
||||
|
@ -28,7 +29,8 @@ public class JSONObjectIntegrationTest {
|
|||
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
|
||||
|
@ -37,6 +39,7 @@ public class JSONObjectIntegrationTest {
|
|||
"{\"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;
|
||||
|
||||
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";
|
||||
|
@ -15,7 +16,7 @@ public class JSONTokenerIntegrationTest {
|
|||
int index = 0;
|
||||
|
||||
while (jt.more()) {
|
||||
assertEquals(expectedTokens[index++], jt.next());
|
||||
assertThat(jt.next()).isEqualTo(expectedTokens[index++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
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();
|
||||
|
@ -14,6 +15,8 @@ public class ObjectToFromJSONIntegrationTest {
|
|||
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}");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue