net.javacrumbs.jsonunit.fluent
Class JsonFluentAssert

java.lang.Object
  extended by net.javacrumbs.jsonunit.fluent.JsonFluentAssert

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:

  1. Jackson JsonNode
  2. Numbers, booleans and any other type parseable by Jackson's ObjectMapper.convertValue
  3. String is parsed as JSON. For expected values the string is quoted if it contains obviously invalid JSON.
  4. Reader similarly to String
  5. null as null Node


Nested Class Summary
Modifier and Type Class and Description
 class JsonFluentAssert.ArrayAssert
          Array assertions
 
Constructor Summary
Modifier Constructor and Description
protected JsonFluentAssert(Object actual)
           
protected JsonFluentAssert(Object actual, String path, String description, Configuration configuration)
           
 
Method Summary
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.
 JsonFluentAssert describedAs(String description)
          Sets the description of this object.
 JsonFluentAssert hasSameStructureAs(Object expected)
          Compares JSON structure.
 JsonFluentAssert ignoring(String ignorePlaceholder)
          Sets the placeholder that can be used to ignore values.
 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.
 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 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonFluentAssert

protected JsonFluentAssert(Object actual,
                           String path,
                           String description,
                           Configuration configuration)

JsonFluentAssert

protected JsonFluentAssert(Object actual)
Method Detail

assertThatJson

public static JsonFluentAssert assertThatJson(Object json)
Creates a new instance of JsonFluentAssert. It is not called assertThat to not clash with StringAssert. The json parameter is converted to JSON using ObjectMapper.

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 use isStringEqualTo(String). If the string parameter is not a valid JSON, it is quoted automatically.

Parameters:
expected -
Returns:
this object.
See Also:
isStringEqualTo(String)

isStringEqualTo

public 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.


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:
this object.

hasSameStructureAs

public JsonFluentAssert hasSameStructureAs(Object expected)
Compares JSON structure. Ignores values, only compares shape of the document and key names.

Parameters:
expected -
Returns:
this object.

node

public JsonFluentAssert node(String path)
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:
path -
Returns:
object comparing only node given by path.

as

public JsonFluentAssert as(String description)
Sets the description of this object.

Parameters:
description -
Returns:

describedAs

public JsonFluentAssert describedAs(String description)
Sets the description of this object.

Parameters:
description -
Returns:

ignoring

public JsonFluentAssert ignoring(String ignorePlaceholder)
Sets the placeholder that can be used to ignore values. The default value is ${json-unit.ignore}

Parameters:
ignorePlaceholder -
Returns:

withTolerance

public JsonFluentAssert withTolerance(double tolerance)
Sets the tolerance for floating number comparison. If set to null, requires exact match of the values. For example, if set to 0.01, ignores all differences lower than 0.01, so 1 and 0.9999 are considered equal.

Parameters:
tolerance -

withTolerance

public JsonFluentAssert withTolerance(BigDecimal tolerance)
Sets the tolerance for floating number comparison. If set to null, requires exact match of the values. For example, if set to 0.01, ignores all differences lower than 0.01, so 1 and 0.9999 are considered equal.

Parameters:
tolerance -

treatingNullAsAbsent

@Deprecated
public JsonFluentAssert treatingNullAsAbsent()
Deprecated. Use when(Option.TREATING_NULL_AS_ABSENT)

When set to true, treats null nodes in actual value as absent. In other words if you expect {"test":{"a":1}} this {"test":{"a":1, "b": null}} will pass the test.

Returns:

when

public JsonFluentAssert when(Option firstOption,
                             Option... otherOptions)
Sets options changing comparison behavior. For more details see Option

Parameters:
firstOption -
otherOptions -
See Also:
Option

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.



Copyright © 2015. All Rights Reserved.