Class JsonUnitRequestMatchers


  • public class JsonUnitRequestMatchers
    extends Object
    Matchers compatible with Spring mocking framework.

    Sample usage:

    mockServer.expect(requestTo(URI)) .andExpect(json().isEqualTo(json)) .andRespond(withSuccess(jsonResponse, MediaType.APPLICATION_JSON_UTF8));

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ME ignoring​(String ignorePlaceholder)
      Sets the placeholder that can be used to ignore values.
      MATCHER isAbsent()
      Fails if the node exists.
      MATCHER isArray()
      Fails if the selected JSON is not an Array or is not present.
      MATCHER isEqualTo​(Object expected)
      Compares JSON for equality.
      MATCHER isFalse()
      Fails if selected JSON is not false.
      MATCHER isNotEqualTo​(Object expected)
      Fails if compared documents are equal.
      MATCHER isNotNull()
      Fails if selected JSON is null.
      MATCHER isNull()
      Fails if selected JSON is not null.
      MATCHER isObject()
      Fails if the selected JSON is not an Object or is not present.
      MATCHER isPresent()
      Fails if the node is missing.
      MATCHER isString()
      Fails if the selected JSON is not a String or is not present.
      MATCHER 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.
      MATCHER isTrue()
      Fails if selected JSON is not true.
      static JsonUnitRequestMatchers json()
      Creates JsonUnitResultMatchers to be used for JSON assertions.
      MATCHER matches​(org.hamcrest.Matcher<?> matcher)
      Matches the node using Hamcrest matcher.
      ME node​(String newPath)
      Creates a matcher object that only compares given node.
      ME when​(net.javacrumbs.jsonunit.core.Option firstOption, net.javacrumbs.jsonunit.core.Option... otherOptions)
      Sets options changing comparison behavior.
      ME withDifferenceListener​(net.javacrumbs.jsonunit.core.listener.DifferenceListener differenceListener)  
      ME withTolerance​(double tolerance)
      Sets the tolerance for floating number comparison.
      ME withTolerance​(BigDecimal tolerance)
      Sets the tolerance for floating number comparison.
    • Method Detail

      • json

        public static JsonUnitRequestMatchers json()
        Creates JsonUnitResultMatchers to be used for JSON assertions.
      • node

        public ME node​(String newPath)
        Creates a matcher object that only compares given node. The path is denoted by JSON path, for example.

        this.mockMvc.perform(get("/sample").accept(MediaType.APPLICATION_JSON)).andExpect(json().node("root.test[0]").isEqualTo("1"));

        Parameters:
        newPath -
        Returns:
        object comparing only node given by path.
      • ignoring

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

        public ME 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.
      • withTolerance

        public ME 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.
      • withDifferenceListener

        public ME withDifferenceListener​(net.javacrumbs.jsonunit.core.listener.DifferenceListener differenceListener)
      • when

        public ME when​(net.javacrumbs.jsonunit.core.Option firstOption,
                       net.javacrumbs.jsonunit.core.Option... otherOptions)
        Sets options changing comparison behavior. For more details see Option
        See Also:
        Option
      • isEqualTo

        public MATCHER 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 MATCHER 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 MATCHER 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.
      • isAbsent

        public MATCHER isAbsent()
        Fails if the node exists.
      • isPresent

        public MATCHER isPresent()
        Fails if the node is missing.
      • isArray

        public MATCHER isArray()
        Fails if the selected JSON is not an Array or is not present.
      • isObject

        public MATCHER isObject()
        Fails if the selected JSON is not an Object or is not present.
      • isString

        public MATCHER isString()
        Fails if the selected JSON is not a String or is not present.
      • isNull

        public MATCHER isNull()
        Fails if selected JSON is not null.
      • isNotNull

        public MATCHER isNotNull()
        Fails if selected JSON is null.
      • matches

        public MATCHER 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:
      • isTrue

        public MATCHER isTrue()
        Fails if selected JSON is not true.
      • isFalse

        public MATCHER isFalse()
        Fails if selected JSON is not false.