Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

EO principles respected here DevOps By Rultor.com

Maven Central Bintray

Build Status Build status CircleCI

PDD status License Hits-of-Code jpeek report Test Coverage

Hamcrest matchers for JSON objects and arrays; adaptors for other types allows you to write single statement unit tests against JSON producing objects.

Usage

Add dependency to your pom.xml:

<dependency>
  <groupId>wtf.g4s8</groupId>
  <artifactId>matchers-json</artifactId>
  <version>1.0.2</version>
</dependency>

you can find latest version on Bintray badge above. Also, this library depends on javax.json:javax.json-api library.

Matching JSON objects

Let's assume you have an object with method:

class Item {
  private final int value;
  
  Item(final int value) {
    this.value = value;
  }
  
  JsonObject json() {
    return Json.createObjectBuilder()
      .add("value", this.value)
      .build()
  }
}

To verify that your object returns correct value in a unit test use JsonHas and JsonValueIs matchers:

@Test()
void returnsJsonWithValue() {
  final int value = 42;
  MatcherAssert.assertThat(
    new Item(value).json(),
    new JsonHas("value", new JsonValueIs(value)))
);

Matching JSON arrays

To match JSON array use JsonContains class with list of matchers for items as argument. E.g. to verify this json array:

[ "foo", 42, true, null ]

use this matcher:

new JsonContains(
    new JsonValueIs("foo"),
    new JsonValueIs(42),
    new JsonValueIs(true),
    JsonValueIs.NULL
);

If your array has JSON objects:

[
  { "value": 1234 },
  { "value": 6532 }
]

use JsonHas matcher inside JsonContains:

new JsonContains(
    new JsonHas("value", new JsonValueIs(1234)),
    new JsonHas("value", new JsonValueIs(6532))
)

You can compose JsonContains, JsonHas and JsonValueIs in any combination. E.g. if you have JSON object:

{
  "items": [
    { "value": 1 },
    { "value": 2 },
    { "value": 3 }
  ]
}

you can use:

new JsonHas(
    "items",
    new JsonContains(
        new JsonHas("value", new JsonValueIs(1)),
        new JsonHas("value", new JsonValueIs(2)),
        new JsonHas("value", new JsonValueIs(3))
    )
)

Matching JSON values

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

For more complex matching you can use primary constructor:

MatcherAssert.assertThat(
    Json.createValue("Starting with 1 2 3"),
    new JsonValueIs(
        JsonValue.ValueType.STRING,
        Matchers.startsWith("Starting")
    )
);

Adapters

Also this library provides some useful classes to help you convert different types to JSON matchers:

StringIsJson is a decorator for JSON matcher which implements Matcher<String> interface, so you can match a string against JSON matchers:

MatcherAssert.assertThat(
    "{\"foo\":\"bar\"}",
    new StringIsJson(new JsonHas("foo", new JsonValueIs("bar")))
);

About

Hamcrest matchers for JSON objects

Topics

Resources

License

Packages

No packages published

Languages