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 |
|---|---|
class |
JsonFluentAssert.ArrayAssert
Array assertions
|
static class |
JsonFluentAssert.JsonFluentAssertAfterAssertion
JsonFluentAssert after assertion.
|
| Modifier and Type | Method and Description |
|---|---|
JsonFluentAssert |
as(String description)
Sets the description of this object.
|
static JsonFluentAssert |
assertThatJson(Object json)
Creates a new instance of
. |
JsonFluentAssert |
describedAs(String description)
Sets the description of this object.
|
JsonFluentAssert.JsonFluentAssertAfterAssertion |
hasSameStructureAs(Object expected)
Compares JSON structure.
|
JsonFluentAssert |
ignoring(String ignorePlaceholder)
Sets the placeholder that can be used to ignore values.
|
JsonFluentAssert.JsonFluentAssertAfterAssertion |
isAbsent()
Fails if the node exists.
|
JsonFluentAssert.ArrayAssert |
isArray()
Fails if the selected JSON is not an Array or is not present.
|
JsonFluentAssert.JsonFluentAssertAfterAssertion |
isEqualTo(Object expected)
Compares JSON for equality.
|
JsonFluentAssert.JsonFluentAssertAfterAssertion |
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.JsonFluentAssertAfterAssertion |
isPresent()
Fails if the node is missing.
|
void |
isString()
Fails if the selected JSON is not a String or is not present.
|
void |
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 matcher.
|
JsonFluentAssert |
node(String path)
Creates an assert object that only compares given node.
|
JsonFluentAssert |
treatingNullAsAbsent()
Deprecated.
Use when(Option.TREATING_NULL_AS_ABSENT)
|
JsonFluentAssert |
when(Option firstOption,
Option... otherOptions)
Sets options changing comparison behavior.
|
JsonFluentAssert |
withTolerance(BigDecimal tolerance)
Sets the tolerance for floating number comparison.
|
JsonFluentAssert |
withTolerance(double tolerance)
Sets the tolerance for floating number comparison.
|
public static JsonFluentAssert 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.JsonFluentAssertAfterAssertion isEqualTo(Object expected)
isStringEqualTo(String).
If the string parameter is not a valid JSON, it is quoted automatically.expected - this object.isStringEqualTo(String)public void isStringEqualTo(String expected)
public JsonFluentAssert.JsonFluentAssertAfterAssertion isNotEqualTo(Object expected)
expected - this object.public JsonFluentAssert.JsonFluentAssertAfterAssertion hasSameStructureAs(Object expected)
expected - this object.public JsonFluentAssert node(String path)
assertThatJson("{\"root\":{\"test\":[1,2,3]}}").node("root.test[0]").isEqualTo("1");
path - public JsonFluentAssert as(String description)
description - public JsonFluentAssert describedAs(String description)
description - public JsonFluentAssert ignoring(String ignorePlaceholder)
ignorePlaceholder - public JsonFluentAssert withTolerance(double tolerance)
tolerance - public JsonFluentAssert withTolerance(BigDecimal tolerance)
tolerance - @Deprecated public JsonFluentAssert treatingNullAsAbsent()
public JsonFluentAssert when(Option firstOption, Option... otherOptions)
OptionfirstOption - otherOptions - Optionpublic JsonFluentAssert.JsonFluentAssertAfterAssertion isAbsent()
public JsonFluentAssert.JsonFluentAssertAfterAssertion isPresent()
public JsonFluentAssert.ArrayAssert isArray()
public void isObject()
public void isString()
public JsonFluentAssert matches(org.hamcrest.Matcher<?> matcher)
matcher - Copyright © 2016. All Rights Reserved.