json-hamcrest-matcher

This is a hamcrest matcher for partially matching json objects.

License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.arusarka
ArtifactId

ArtifactId

json-hamcrest-matcher
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

json-hamcrest-matcher
This is a hamcrest matcher for partially matching json objects.
Source Code Management

Source Code Management

https://github.com/arusarka/json-hamcrest-matcher/tree/master

Download json-hamcrest-matcher

How to add to project

<!-- https://jarcasting.com/artifacts/com.arusarka/json-hamcrest-matcher/ -->
<dependency>
    <groupId>com.arusarka</groupId>
    <artifactId>json-hamcrest-matcher</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.arusarka/json-hamcrest-matcher/
implementation 'com.arusarka:json-hamcrest-matcher:1.0'
// https://jarcasting.com/artifacts/com.arusarka/json-hamcrest-matcher/
implementation ("com.arusarka:json-hamcrest-matcher:1.0")
'com.arusarka:json-hamcrest-matcher:jar:1.0'
<dependency org="com.arusarka" name="json-hamcrest-matcher" rev="1.0">
  <artifact name="json-hamcrest-matcher" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.arusarka', module='json-hamcrest-matcher', version='1.0')
)
libraryDependencies += "com.arusarka" % "json-hamcrest-matcher" % "1.0"
[com.arusarka/json-hamcrest-matcher "1.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.1.5
org.hamcrest : hamcrest-core jar 1.3

test (2)

Group / Artifact Type Version
junit : junit jar 4.11
org.mockito : mockito-core jar 1.9.5

Project Modules

There are no modules declared in this project.

Hamcrest Matcher for JSON

This library provides a hamcrest matcher for json. The idea of this matcher is to provide partial matching of json objects. The most important aspect of the matcher is that the relaxed (or partial) matching style works at any level nested inside a json object. What that means is, as long as the expected json is contained inside the actual json, it would match.

Maven dependency

<dependency>
    <groupId>com.arusarka</groupId>
    <artifactId>json-hamcrest-matcher</artifactId>
    <version>1.0</version>
</dependency>

Basic usage

Using with JUnit 4

Assert.assertThat(actual, JSONMatcher.shouldContainJson(expected));

where actual and expected are both json string variables.

Given below are a bunch of different cases for which the matcher will work.

Json object attribute absent in expected

Following jsons would match since the matcher does a partial match.

Actual Expected
{"foo" : "bar","baz" : "qux"} {"foo" : "bar"}

However, the following would not.

Actual Expected
{"foo" : "bar"} {"foo" : "bar","bar" : "qux"}

Json arrays matching

The matcher is very relaxed about array matching. It would match if expected array elements match partially with any element inside actual json array. It does not require that the sizes of expected and actual array to match. Nor does not require the order to be the same.

Following jsons would match.

Actual Expected
[ { "foo": "bar", "bar": "qux" }, { "hello": "world", "lorem": "ipsum" } ] [ { "foo": "bar", "bar": "qux" } ]
[ { "foo": "bar", "bar": "qux" } ] [ { "foo": "bar" } ]

Versions

Version
1.0