public class JsonFluentAssert extends Object
assertThatJson("{\"test\":1}").isEqualTo("{\"test\":2}");
assertThatJson("{\"test\":1}").hasSameStructureAs("{\"test\":21}");
assertThatJson("{\"root\":{\"test\":1}}").node("root.test").isEqualTo("2");
Please note that the method name is assertThatJson and not assertThat. The reason is that we need to accept String parameter
and do not want to override standard FEST or AssertJ assertThat(String) method.
All the methods accept Objects as parameters. The supported types are:
Reader similarly to String| Modifier and Type | Class and Description |
|---|---|
static class |
JsonFluentAssert.ArrayAssert
Array assertions
|
static class |
JsonFluentAssert.ConfigurableJsonFluentAssert
JsonFluentAssert that can be configured.
|
| Modifier and Type | Method and Description |
|---|---|
static JsonFluentAssert.ConfigurableJsonFluentAssert |
assertThatJson(Object json)
Creates a new instance of
. |
JsonFluentAssert |
hasSameStructureAs(Object expected)
Deprecated.
Use IGNORING_VALUES option instead
|
JsonFluentAssert |
isAbsent()
Fails if the node exists.
|
JsonFluentAssert.ArrayAssert |
isArray()
Fails if the selected JSON is not an Array or is not present.
|
JsonFluentAssert |
isEqualTo(Object expected)
Compares JSON for equality.
|
JsonFluentAssert |
isNotEqualTo(Object expected)
Fails if compared documents are equal.
|
void |
isObject()
Fails if the selected JSON is not an Object or is not present.
|
JsonFluentAssert |
isPresent()
Fails if the node is missing.
|
void |
isString()
Fails if the selected JSON is not a String or is not present.
|
JsonFluentAssert |
isStringEqualTo(String expected)
Fails if the selected JSON is not a String or is not present or the value
is not equal to expected value.
|
JsonFluentAssert |
matches(org.hamcrest.Matcher<?> matcher)
Matches the node using Hamcrest internalMatcher.
|
JsonFluentAssert |
node(String newPath)
Creates an assert object that only compares given node.
|
public static JsonFluentAssert.ConfigurableJsonFluentAssert assertThatJson(Object json)
JsonFluentAssert.
It is not called assertThat to not clash with StringAssert.
The json parameter is converted to JSON using ObjectMapper.json - public JsonFluentAssert isEqualTo(Object expected)
isStringEqualTo(String).
If the string parameter is not a valid JSON, it is quoted automatically.expected - this object.isStringEqualTo(String)public JsonFluentAssert isStringEqualTo(String expected)
public JsonFluentAssert isNotEqualTo(Object expected)
expected - this object.@Deprecated public JsonFluentAssert hasSameStructureAs(Object expected)
expected - this object.public JsonFluentAssert node(String newPath)
assertThatJson("{\"root\":{\"test\":[1,2,3]}}").node("root.test[0]").isEqualTo("1");
newPath - public JsonFluentAssert isAbsent()
public JsonFluentAssert isPresent()
public JsonFluentAssert.ArrayAssert isArray()
public void isObject()
public void isString()
public JsonFluentAssert matches(org.hamcrest.Matcher<?> matcher)
matcher - Copyright © 2019. All rights reserved.