BAEL-722 Intro to JSONassert (#1437)
* yasin.bhojawala@gmail.com Evaluation article on Different Types of Bean Injection in Spring * Revert "yasin.bhojawala@gmail.com" This reverts commit 963cc51a7a15b75b550108fe4e198cd65a274032. * Fixing compilation error and removing unused import * Introduction to Java9 StackWalking API - yasin.bhojawala@gmail.com Code examples for the article "Introduction to Java9 StackWalking API" * BAEL-608 Introduction to Java9 StackWalking API * BAEL-608 Introduction to Java9 StackWalking API changing the test names to BDD style * BAEL-608 Introduction to Java9 StackWalking API correcting the typo * BAEL-608 Introduction to Java9 StackWalking API improving method names * BAEL-608 Introduction to Java9 StackWalking API test method names improvements * BAEL-718 Quick intro to javatuples * merging pom from master * BAEL-722 Intro to JSONassert
This commit is contained in:
parent
22b5c4924b
commit
9d0cb1e2aa
@ -1,77 +1,83 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>libraries</artifactId>
|
<artifactId>libraries</artifactId>
|
||||||
<name>libraries</name>
|
<name>libraries</name>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>1.8</source>
|
||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
|
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cglib</groupId>
|
<groupId>cglib</groupId>
|
||||||
<artifactId>cglib</artifactId>
|
<artifactId>cglib</artifactId>
|
||||||
<version>${cglib.version}</version>
|
<version>${cglib.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang.version}</version>
|
<version>${commons-lang.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jasypt</groupId>
|
<groupId>org.jasypt</groupId>
|
||||||
<artifactId>jasypt</artifactId>
|
<artifactId>jasypt</artifactId>
|
||||||
<version>${jasypt.version}</version>
|
<version>${jasypt.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.javatuples</groupId>
|
<groupId>org.javatuples</groupId>
|
||||||
<artifactId>javatuples</artifactId>
|
<artifactId>javatuples</artifactId>
|
||||||
<version>${javatuples.version}</version>
|
<version>${javatuples.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.javassist</groupId>
|
<groupId>org.javassist</groupId>
|
||||||
<artifactId>javassist</artifactId>
|
<artifactId>javassist</artifactId>
|
||||||
<version>${javaassist.version}</version>
|
<version>${javaassist.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
|
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>org.skyscreamer</groupId>
|
||||||
|
<artifactId>jsonassert</artifactId>
|
||||||
|
<version>${jsonassert.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<cglib.version>3.2.4</cglib.version>
|
<cglib.version>3.2.4</cglib.version>
|
||||||
<commons-lang.version>3.5</commons-lang.version>
|
<commons-lang.version>3.5</commons-lang.version>
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<jasypt.version>1.9.2</jasypt.version>
|
<jasypt.version>1.9.2</jasypt.version>
|
||||||
<javatuples.version>1.2</javatuples.version>
|
<javatuples.version>1.2</javatuples.version>
|
||||||
<javaassist.version>3.21.0-GA</javaassist.version>
|
<javaassist.version>3.21.0-GA</javaassist.version>
|
||||||
<assertj.version>3.6.2</assertj.version>
|
<assertj.version>3.6.2</assertj.version>
|
||||||
</properties>
|
<jsonassert.version>1.4.0</jsonassert.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,115 @@
|
|||||||
|
package com.baeldung.jsonassert;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.skyscreamer.jsonassert.Customization;
|
||||||
|
import org.skyscreamer.jsonassert.JSONAssert;
|
||||||
|
import org.skyscreamer.jsonassert.JSONCompareMode;
|
||||||
|
import org.skyscreamer.jsonassert.RegularExpressionValueMatcher;
|
||||||
|
import org.skyscreamer.jsonassert.comparator.ArraySizeComparator;
|
||||||
|
import org.skyscreamer.jsonassert.comparator.CustomComparator;
|
||||||
|
|
||||||
|
public class JsonAssertTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenLenientode_whenAssertEqualsSameJsonString_thenPass() throws JSONException {
|
||||||
|
String actual = "{id:123,name:\"John\"}";
|
||||||
|
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT);
|
||||||
|
|
||||||
|
actual = "{id:123,name:\"John\",zip:\"33025\"}";
|
||||||
|
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStrictMode_whenAssertNotEqualsExtendedJsonString_thenPass() throws JSONException {
|
||||||
|
String actual = "{id:123,name:\"John\"}";
|
||||||
|
JSONAssert.assertNotEquals("{name:\"John\"}", actual, JSONCompareMode.STRICT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingCompareModeOrBoolean_thenBothAreSame() throws JSONException {
|
||||||
|
String actual = "{id:123,name:\"John\",zip:\"33025\"}";
|
||||||
|
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT);
|
||||||
|
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, false);
|
||||||
|
|
||||||
|
actual = "{id:123,name:\"John\"}";
|
||||||
|
JSONAssert.assertNotEquals("{name:\"John\"}", actual, JSONCompareMode.STRICT);
|
||||||
|
JSONAssert.assertNotEquals("{name:\"John\"}", actual, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDifferentOrderForJsonObject_whenAssertEquals_thenPass() throws JSONException {
|
||||||
|
String result = "{id:1,name:\"John\"}";
|
||||||
|
|
||||||
|
JSONAssert.assertEquals("{name:\"John\",id:1}", result, JSONCompareMode.STRICT);
|
||||||
|
JSONAssert.assertEquals("{name:\"John\",id:1}", result, JSONCompareMode.LENIENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDifferentTypes_whenAssertEqualsSameValue_thenPass() throws JSONException {
|
||||||
|
JSONObject expected = new JSONObject();
|
||||||
|
JSONObject actual = new JSONObject();
|
||||||
|
expected.put("id", Integer.valueOf(12345));
|
||||||
|
actual.put("id", Double.valueOf(12345));
|
||||||
|
|
||||||
|
JSONAssert.assertEquals(expected, actual, false);
|
||||||
|
JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNestedObjects_whenAssertEquals_thenPass() throws JSONException {
|
||||||
|
String result = "{id:1,name:\"Juergen\", address:{city:\"Hollywood\", "
|
||||||
|
+ "state:\"LA\", zip:91601}}";
|
||||||
|
JSONAssert.assertEquals("{id:1,name:\"Juergen\", address:{city:\"Hollywood\", "
|
||||||
|
+ "state:\"LA\", zip:91601}}", result, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray_whenComparing_thenOrderMustMatchForStrict() throws JSONException {
|
||||||
|
String result = "[Alex, Barbera, Charlie, Xavier]";
|
||||||
|
JSONAssert.assertEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.LENIENT);
|
||||||
|
JSONAssert.assertEquals("[Alex, Barbera, Charlie, Xavier]", result, JSONCompareMode.STRICT);
|
||||||
|
JSONAssert.assertNotEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.STRICT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray_whenComparingExtended_thenNotEqual() throws JSONException {
|
||||||
|
String result = "[1,2,3,4,5]";
|
||||||
|
JSONAssert.assertEquals("[1,2,3,4,5]", result, JSONCompareMode.LENIENT);
|
||||||
|
JSONAssert.assertNotEquals("[1,2,3]", result, JSONCompareMode.LENIENT);
|
||||||
|
JSONAssert.assertNotEquals("[1,2,3,4,5,6]", result, JSONCompareMode.LENIENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenComparingSizeOfArray_thenPass() throws JSONException {
|
||||||
|
String names = "{names:[Alex, Barbera, Charlie, Xavier]}";
|
||||||
|
JSONAssert.assertEquals(
|
||||||
|
"{names:[4]}",
|
||||||
|
names,
|
||||||
|
new ArraySizeComparator(JSONCompareMode.LENIENT));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenComparingContentsOfArray_thenPass() throws JSONException {
|
||||||
|
String ratings = "{ratings:[3.2,3.5,4.1,5,1]}";
|
||||||
|
JSONAssert.assertEquals(
|
||||||
|
"{ratings:[1,5]}",
|
||||||
|
ratings,
|
||||||
|
new ArraySizeComparator(JSONCompareMode.LENIENT));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValueMatcher_whenComparingUsingRegex_thenPass() throws IllegalArgumentException, JSONException {
|
||||||
|
JSONAssert.assertEquals("{entry:{id:x}}", "{entry:{id:1, id:2}}",
|
||||||
|
new CustomComparator(
|
||||||
|
JSONCompareMode.STRICT,
|
||||||
|
new Customization("entry.id",
|
||||||
|
new RegularExpressionValueMatcher<Object>("\\d"))));
|
||||||
|
|
||||||
|
JSONAssert.assertNotEquals("{entry:{id:x}}", "{entry:{id:1, id:as}}",
|
||||||
|
new CustomComparator(JSONCompareMode.STRICT,
|
||||||
|
new Customization("entry.id",
|
||||||
|
new RegularExpressionValueMatcher<Object>("\\d"))));
|
||||||
|
}
|
||||||
|
}
|
0
xml/src/main/resources/customer-binding.xml
Executable file → Normal file
0
xml/src/main/resources/customer-binding.xml
Executable file → Normal file
Loading…
x
Reference in New Issue
Block a user