Class JSONMatchers


  • public class JSONMatchers
    extends Object
    Provides different matchers for asserting JSONObjects and JSONArrays
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONObject> equal​(org.codehaus.jettison.json.JSONObject expected)
      Creates a matcher for a JSONObject matching when the examined JSONObject has exactly the same number of properties with the same values as the expected one.
      static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONArray> hasItems​(Object... expected)
      Creates a matcher for a JSONArray matching when the examined JSONArray contains all the expected objects.
      static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONArray> hasItems​(org.hamcrest.Matcher<?>... expected)
      Creates a matcher for a JSONArray matching when the examined JSONArray matches with all the expected matchers.
      static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONObject> matchesObject​(org.codehaus.jettison.json.JSONObject expected)
      Creates a matcher for a JSONObject matching when the examined JSONObject contains the properties with the same values of the expected one.
    • Method Detail

      • equal

        public static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONObject> equal​(org.codehaus.jettison.json.JSONObject expected)
        Creates a matcher for a JSONObject matching when the examined JSONObject has exactly the same number of properties with the same values as the expected one. The order of the keys is not taken into account. It accepts matcher properties. For example assertThat(actual, equal(expected));: - Passes if actual = {p1: "hello", p2: 2} and expected = {p1: "hello", p2: 2}
        - Passes if actual = {p1: "hello", p2: 2} and expected = {p1: "hello", p2: greaterThan(1)}
        - Does not pass if actual = {p1: "hello", p2: 2} and expected = {p1: "hello", p2: 1}
        Parameters:
        expected - the JSONObject that is expected to be equal to the actual one
        Returns:
        the equal matcher
      • matchesObject

        public static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONObject> matchesObject​(org.codehaus.jettison.json.JSONObject expected)
        Creates a matcher for a JSONObject matching when the examined JSONObject contains the properties with the same values of the expected one. The order of the keys is not taken into account. It accepts matcher properties. For example assertThat(actual, matchesObject(expected));: - Passes if actual = {p1: "hello", p2: 2} and expected = {p1: "hello", p2: 2}
        - Passes if actual = {p1: "hello", p2: 2} and expected = {p1: "hello", p2: greaterThan(1)}
        - Passes if actual = {p1: "hello", p2: 2} and expected = {p1: "hello"}
        - Passes if actual = {p1: "hello", p2: 2} and expected = {p2: greaterThan(1)}
        - Does not pass if actual = {p1: "hello", p2: 2} and expected = {p1: "hello", p2: 1}
        - Does not pass if actual = {p1: "hello", p2: 2} and expected = {p1: "hello", p2: 2, p3: "bye"}
        Parameters:
        expected - the JSON object that is expected to match all its properties with the actual one
        Returns:
        the matchesObject matcher
      • hasItems

        public static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONArray> hasItems​(Object... expected)
        Creates a matcher for a JSONArray matching when the examined JSONArray contains all the expected objects. The order of the objects is not taken into account. For example assertThat(actual, hasItems(item1, item2));: - Passes if actual = [{p1: "hello", p2: 2}, 1], item = {p1: "hello", p2: 2} and item2 = 1
        - Passes if actual = [{p1: "hello", p2: 2}, 1], item = {p1: "hello", p2: greaterThan(1)} and item2 = 1
        - Does not pass if actual = [{p1: "hello", p2: 2}, 1], item = {p1: "hello", p2: 2} and item2 = 3
        Parameters:
        expected - the objects that are expected to be included in the actual JSONArray
        Returns:
        the hasItems for objects matcher
      • hasItems

        @SafeVarargs
        public static org.hamcrest.Matcher<org.codehaus.jettison.json.JSONArray> hasItems​(org.hamcrest.Matcher<?>... expected)
        Creates a matcher for a JSONArray matching when the examined JSONArray matches with all the expected matchers. For example assertThat(actual, hasItems(matcher1, matcher2));: - Passes if actual = [{p1: "hello", p2: 2}, 1], matcher1 = matchesObject{p1: "hello"} and matcher2 = greaterThan(1)
        - Passes if actual = [{p1: "hello", p2: 2}, 1], matcher1 = matchesObject{p1: startsWith("he")} and matcher2 = greaterThan(1)
        - Does not pass if actual = [{p1: "hello", p2: 2}, 1], matcher1 = matchesObject{p1: "hello"} and matcher2 = greaterThan(10)
        Parameters:
        expected - a list of matchers that are expected to match with the actual JSONArray
        Returns:
        the hasItems for matchers matcher