Skip to content

Latest commit

 

History

History
62 lines (50 loc) · 2.38 KB

File metadata and controls

62 lines (50 loc) · 2.38 KB

matchers-json

Hamcrest matchers for json objects

EO principles respected here DevOps By Rultor.com

Bintray Build Status Build status PDD status License Test Coverage

Motivation

This library helps to achieve single statement unit test rule, when testing JSON objects.

Download

To use it add maven dependency to your pom.xml:

<dependency>
  <groupId>com.g4s8</groupId>
  <artifactId>matchers-json</artifactId>
</dependency>

last version is: Bintray

Usage

You can find examples in src/test files.

To match a field in JSON object use JsonHas:

MatcherAssert.assertThat(
    Json.createObjectBuilder()
        .add(
            "foo",
            Json.createObjectBuilder()
              .add("bar", 42)
              .build()
        ).build(),
    new JsonHas(
        "foo",
        new JsonHas(
            "bar",
            new JsonValueIs(42)
        )
    )
);

To match json value use JsonValueIs:

  • for strings: new JsonValueIs("some string")
  • for numbers: new JsonValueIs(100)
  • for booleans: new JsonValueIs(true)
  • for json-null: JsonValueIs.NULL

To match JSON array items use JsonContains (not implemented yet: g4s8#2)