Hamcrest matchers for json objects
This library helps to achieve single statement unit test rule, when testing JSON objects.
To use it add maven dependency to your pom.xml
:
<dependency>
<groupId>com.g4s8</groupId>
<artifactId>matchers-json</artifactId>
</dependency>
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)