Package net.javacrumbs.jsonunit.fluent
Class JsonFluentAssert
- java.lang.Object
-
- net.javacrumbs.jsonunit.fluent.JsonFluentAssert
-
- Direct Known Subclasses:
JsonFluentAssert.ConfigurableJsonFluentAssert
public class JsonFluentAssert extends Object
Contains JSON related fluent assertions inspired by FEST or AssertJ. Typical usage is: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:- Jackson JsonNode
- Gson JsonElement
- Numbers, booleans and any other type parsable by underlying JSON library
- String is parsed as JSON. For expected values the string is quoted if it contains obviously invalid JSON.
Readersimilarly to String- null as null Node
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classJsonFluentAssert.ArrayAssertArray assertionsstatic classJsonFluentAssert.ConfigurableJsonFluentAssertJsonFluentAssert that can be configured.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static JsonFluentAssert.ConfigurableJsonFluentAssertassertThatJson(Object json)Creates a new instance of.JsonFluentAssertJsonFluentAsserthasSameStructureAs(Object expected)Deprecated.Use IGNORING_VALUES option insteadJsonFluentAssertisAbsent()Fails if the node exists.JsonFluentAssert.ArrayAssertisArray()Fails if the selected JSON is not an Array or is not present.JsonFluentAssertisEqualTo(Object expected)Compares JSON for equality.JsonFluentAssertisNotEqualTo(Object expected)Fails if compared documents are equal.voidisObject()Fails if the selected JSON is not an Object or is not present.JsonFluentAssertisPresent()Fails if the node is missing.voidisString()Fails if the selected JSON is not a String or is not present.JsonFluentAssertisStringEqualTo(String expected)Fails if the selected JSON is not a String or is not present or the value is not equal to expected value.JsonFluentAssertmatches(org.hamcrest.Matcher<?> matcher)Matches the node using Hamcrest matcher.JsonFluentAssertnode(String newPath)Creates an assert object that only compares given node.
-
-
-
Method Detail
-
assertThatJson
public static JsonFluentAssert.ConfigurableJsonFluentAssert assertThatJson(Object json)
Creates a new instance of. It is not called assertThat to not clash with StringAssert. The json parameter is converted to JSON using ObjectMapper.JsonFluentAssert- Parameters:
json-- Returns:
- new JsonFluentAssert object.
-
isEqualTo
public JsonFluentAssert isEqualTo(Object expected)
Compares JSON for equality. The expected object is converted to JSON before comparison. Ignores order of sibling nodes and whitespaces. Please note that if you pass a String, it's parsed as JSON which can lead to an unexpected behavior. If you pass in "1" it is parsed as a JSON containing integer 1. If you compare it with a string it fails due to a different type. If you want to pass in real string you have to quote it "\"1\"" or useisStringEqualTo(String). If the string parameter is not a valid JSON, it is quoted automatically.- Parameters:
expected-- Returns:
thisobject.- See Also:
isStringEqualTo(String)
-
isStringEqualTo
public 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.
-
isNotEqualTo
public JsonFluentAssert isNotEqualTo(Object expected)
Fails if compared documents are equal. The expected object is converted to JSON before comparison. Ignores order of sibling nodes and whitespaces.- Parameters:
expected-- Returns:
thisobject.
-
hasSameStructureAs
@Deprecated public JsonFluentAssert hasSameStructureAs(Object expected)
Deprecated.Use IGNORING_VALUES option insteadCompares JSON structure. Ignores values, only compares shape of the document and key names. Is too lenient, ignores types, prefer IGNORING_VALUES option instead.- Parameters:
expected-- Returns:
thisobject.
-
node
public JsonFluentAssert node(String newPath)
Creates an assert object that only compares given node. The path is denoted by JSON path, for example.assertThatJson("{\"root\":{\"test\":[1,2,3]}}").node("root.test[0]").isEqualTo("1");- Parameters:
newPath-- Returns:
- object comparing only node given by path.
-
isAbsent
public JsonFluentAssert isAbsent()
Fails if the node exists.- Returns:
-
isPresent
public JsonFluentAssert isPresent()
Fails if the node is missing.
-
isArray
public JsonFluentAssert.ArrayAssert isArray()
Fails if the selected JSON is not an Array or is not present.- Returns:
-
isObject
public void isObject()
Fails if the selected JSON is not an Object or is not present.
-
isString
public void isString()
Fails if the selected JSON is not a String or is not present.
-
matches
public JsonFluentAssert matches(org.hamcrest.Matcher<?> matcher)
Matches the node using Hamcrest matcher.- Numbers are mapped to BigDecimal
- Arrays are mapped to a Collection
- Objects are mapped to a map so you can use json(Part)Equals or a Map matcher
- Parameters:
matcher-- Returns:
-
-