Class WebTestClientJsonMatcher

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

public class WebTestClientJsonMatcher extends Object
Matcher compatible with Spring TestWebClient.

Sample usage:

client.get().uri(path).accept(MediaType.APPLICATION_JSON).exchange().expectBody() .consumeWith(json().isEqualTo(CORRECT_JSON));

  • Method Summary

    Modifier and Type
    Method
    Description
    ignoring(String ignorePlaceholder)
    Sets the placeholder that can be used to ignore values.
    inPath(String path)
    Uses JsonPath to extract values from the actual value.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if the node exists.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if the selected JSON is not an Array or is not present.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    isEqualTo(@Nullable Object expected)
    Compares JSON for equality.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if selected JSON is not false.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    isNotEqualTo(@Nullable Object expected)
    Fails if compared documents are equal.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if selected JSON is null.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if selected JSON is not null.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if the selected JSON is not an Object or is not present.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if the node is missing.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if the selected JSON is not a String or is not present.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    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.
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    Fails if selected JSON is not true.
     
    matchers(net.javacrumbs.jsonunit.core.Configuration configuration)
     
    Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>>
    matches(org.hamcrest.Matcher<?> matcher)
    Matches the node using Hamcrest matcher.
    node(String path)
    Creates a matcher object that only compares given node.
    when(net.javacrumbs.jsonunit.core.ConfigurationWhen.PathsParam object, net.javacrumbs.jsonunit.core.ConfigurationWhen.ApplicableForPath... actions)
    Adds path specific options.
    when(net.javacrumbs.jsonunit.core.Option firstOption, net.javacrumbs.jsonunit.core.Option... otherOptions)
    Sets options changing comparison behavior.
    withDifferenceListener(net.javacrumbs.jsonunit.core.listener.DifferenceListener differenceListener)
     
    withMatcher(String matcherName, 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 Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • json

      public static WebTestClientJsonMatcher json()
    • matchers

      protected WebTestClientJsonMatcher matchers(net.javacrumbs.jsonunit.core.Configuration configuration)
    • node

      public WebTestClientJsonMatcher node(String path)
      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"));

      Returns:
      object comparing only node given by path.
    • inPath

      public WebTestClientJsonMatcher inPath(String path)
      Uses JsonPath to extract values from the actual value.
    • ignoring

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

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

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

      public WebTestClientJsonMatcher withTolerance(@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

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

      public WebTestClientJsonMatcher 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
    • when

      public WebTestClientJsonMatcher when(net.javacrumbs.jsonunit.core.ConfigurationWhen.PathsParam object, net.javacrumbs.jsonunit.core.ConfigurationWhen.ApplicableForPath... actions)
      Adds path specific options.
      See Also:
      • Configuration.when(ConfigurationWhen.PathsParam, ConfigurationWhen.ApplicableForPath...)
    • isEqualTo

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isEqualTo(@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.

      Returns:
      this object.
      See Also:
    • isStringEqualTo

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> 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.
    • isNotEqualTo

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isNotEqualTo(@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

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isAbsent()
      Fails if the node exists.
    • isPresent

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isPresent()
      Fails if the node is missing.
    • isArray

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isArray()
      Fails if the selected JSON is not an Array or is not present.
    • isObject

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isObject()
      Fails if the selected JSON is not an Object or is not present.
    • isString

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isString()
      Fails if the selected JSON is not a String or is not present.
    • isNull

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isNull()
      Fails if selected JSON is not null.
    • isNotNull

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isNotNull()
      Fails if selected JSON is null.
    • matches

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> 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
    • isTrue

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isTrue()
      Fails if selected JSON is not true.
    • isFalse

      public Consumer<org.springframework.test.web.reactive.server.EntityExchangeResult<byte[]>> isFalse()
      Fails if selected JSON is not false.