java.lang.Object
net.javacrumbs.jsonunit.fluent.JsonFluentAssert
- Direct Known Subclasses:
JsonFluentAssert.ConfigurableJsonFluentAssert
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 ClassesModifier and TypeClassDescriptionstatic classArray assertionsstatic classJsonFluentAssert that can be configured. -
Method Summary
Modifier and TypeMethodDescriptionassertThatJson(Object json) Creates a new instance of.JsonFluentAssertisAbsent()Fails if the node exists.isArray()Fails if the selected JSON is not an Array or is not present.Compares JSON for equality.isNotEqualTo(Object expected) Fails if compared documents are equal.voidisObject()Fails if the selected JSON is not an Object or is not present.Fails if the node is missing.voidisString()Fails if the selected JSON is not a String or is not present.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.matches(org.hamcrest.Matcher<?> matcher) Matches the node using Hamcrest internalMatcher.Creates an assert object that only compares given node.
-
Method Details
-
assertThatJson
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
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
Fails if the selected JSON is not a String or is not present or the value is not equal to expected value. -
isNotEqualTo
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.
-
node
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
Fails if the node exists.- Returns:
-
isPresent
Fails if the node is missing. -
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
Matches the node using Hamcrest internalMatcher.- 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 internalMatcher
- Parameters:
matcher-- Returns:
-