Class JsonUnitResultMatchers

java.lang.Object
net.javacrumbs.jsonunit.spring.JsonUnitResultMatchers

public class JsonUnitResultMatchers extends Object
Matchers compatible with Spring MVC test framework.

Sample usage:

this.mockMvc.perform(get("/sample").accept(MediaType.APPLICATION_JSON)).andExpect(json().isEqualTo(CORRECT_JSON));

  • Method Summary

    Modifier and Type
    Method
    Description
    ignoring(@NotNull String ignorePlaceholder)
    Sets the placeholder that can be used to ignore values.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if the node exists.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if the selected JSON is not an Array or is not present.
    org.springframework.test.web.servlet.ResultMatcher
    isEqualTo(@Nullable Object expected)
    Compares JSON for equality.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if selected JSON is not false.
    org.springframework.test.web.servlet.ResultMatcher
    isNotEqualTo(@Nullable Object expected)
    Fails if compared documents are equal.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if selected JSON is null.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if selected JSON is not null.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if the selected JSON is not an Object or is not present.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if the node is missing.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if the selected JSON is not a String or is not present.
    org.springframework.test.web.servlet.ResultMatcher
    isStringEqualTo(@Nullable String expected)
    Fails if the selected JSON is not a String or is not present or the value is not equal to expected value.
    org.springframework.test.web.servlet.ResultMatcher
    Fails if selected JSON is not true.
    Creates JsonUnitResultMatchers to be used for JSON assertions.
    org.springframework.test.web.servlet.ResultMatcher
    matches(@NotNull org.hamcrest.Matcher<?> matcher)
    Matches the node using Hamcrest matcher.
    node(String newPath)
    Creates a matcher object that only compares given node.
    when(@NotNull net.javacrumbs.jsonunit.core.Option firstOption, @NotNull net.javacrumbs.jsonunit.core.Option... otherOptions)
    Sets options changing comparison behavior.
    withDifferenceListener(@NotNull net.javacrumbs.jsonunit.core.listener.DifferenceListener differenceListener)
     
    withMatcher(@NotNull String matcherName, @NotNull org.hamcrest.Matcher<?> matcher)
    Adds a matcher to be used in ${json-unit.matches:matcherName} macro.
    withTolerance(double tolerance)
    Sets the tolerance for floating number comparison.
    withTolerance(@Nullable BigDecimal 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
  • Method Details

    • json

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

      @NotNull public JsonUnitResultMatchers 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

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

      @NotNull public JsonUnitResultMatchers 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.
    • withMatcher

      @NotNull public JsonUnitResultMatchers withMatcher(@NotNull @NotNull String matcherName, @NotNull @NotNull org.hamcrest.Matcher<?> matcher)
      Adds a matcher to be used in ${json-unit.matches:matcherName} macro.
    • withTolerance

      @NotNull public JsonUnitResultMatchers withTolerance(@Nullable @Nullable 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

      @NotNull public JsonUnitResultMatchers withDifferenceListener(@NotNull @NotNull net.javacrumbs.jsonunit.core.listener.DifferenceListener differenceListener)
    • when

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

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isEqualTo(@Nullable @Nullable 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

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isStringEqualTo(@Nullable @Nullable 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

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isNotEqualTo(@Nullable @Nullable 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

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isAbsent()
      Fails if the node exists.
    • isPresent

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isPresent()
      Fails if the node is missing.
    • isArray

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isArray()
      Fails if the selected JSON is not an Array or is not present.
    • isObject

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isObject()
      Fails if the selected JSON is not an Object or is not present.
    • isString

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isString()
      Fails if the selected JSON is not a String or is not present.
    • isNull

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isNull()
      Fails if selected JSON is not null.
    • isNotNull

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isNotNull()
      Fails if selected JSON is null.
    • matches

      @NotNull public org.springframework.test.web.servlet.ResultMatcher matches(@NotNull @NotNull 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

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isTrue()
      Fails if selected JSON is not true.
    • isFalse

      @NotNull public org.springframework.test.web.servlet.ResultMatcher isFalse()
      Fails if selected JSON is not false.