Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
/ jsonmatcher Public archive

JSON matchers based on hamcrest matcher framework (for org.json)

Notifications You must be signed in to change notification settings

sudhirbits/jsonmatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

JSON matchers based on hamcrest matcher framework (for org.json)

Hamcrest framework extension to add support for JSON. The target is to build a custom factory of Matcher to aid in unit tests and production code.

Introduction Pages and pages of unit tests for verification of JSONObjects and inclination of using Hamcrest matchers have resulted in this.

Details

  1. hasKeyMatcher - returns a Matcher which can be used for development/tests.
JSONObject json = new JSONObject("{" +
  "key:value" +
"}");
assertThat(json, hasKey("key"));


JSONObject json = new JSONObject("{" +                              
  "startsWithStartContainsAndEndsWithend:value" + 
"}";
assertThat(json, hasKey( 
  startsWith("start"),
  endsWith("end"),   
  containsString("Contains")
));
  1. hasPathMatcher - exposes a matcher API hasPath that takes in a JSON path (as string) and returns a Matcher The path is similar to bean path of java object graph. Samples
  • root.node1.node2.node3
  • root.node1[3].node2.node3[0][1].node4
  • root.node1.[3].node2[0].[6]
  • and so on.
public static final String JSON_STRING_FOR_PATH_TEST = 
  "{" +
    "json1: {" +
      "json2: {" +
        "json3: [" +
          "{" +
            "field:value" + 
          "}" +
        "], " +
      "json4: {" +
        "field4:value4" +
      "}" +
    "}" + 
  "}" + 
"}";

@SuppressWarnings("unchecked")
@Test
public void verifyThatMatcherIsAbleToLocatePath() throws JSONException {                
  JSONObject json = new JSONObject(JSON_STRING_FOR_PATH_TEST);
  assertThat(json, allOf(
    hasPath("json1.json2.json3[0].field"), 
    hasPath("json1.json2.json4.field4")));
}
                
@Test
public void verifyThatMatcherReturnsFalseWhenPathIsNotFound() throws JSONException {            
  JSONObject json = new JSONObject(JSON_STRING_FOR_PATH_TEST);
  assertThat(hasPath("json1.json2.json3[8].field").matches(json), is(false));
}

Dependencies

  1. hamcrest
    <dependency>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
          <version>1.3</version>
    </dependency>
    <dependency>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-library</artifactId>
          <version>1.3</version>
    </dependency>
  1. google guava libraries
    <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>14.0.1</version>
    </dependency>
  1. org.json 20090211
    <dependency>
          <groupId>org.json</groupId>
          <artifactId>json</artifactId>
          <version>20090211</version>
    </dependency>
  1. junit 4.11 (tests only)
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

Source Code Quality on Codacy Codacy Badge

About

JSON matchers based on hamcrest matcher framework (for org.json)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages