https://github.com/qaware/majx
Matching JSON expressively
https://github.com/qaware/majx
hacktoberfest json jvm kotlin mustache pattern-matching test
Last synced: about 1 year ago
JSON representation
Matching JSON expressively
- Host: GitHub
- URL: https://github.com/qaware/majx
- Owner: qaware
- License: mit
- Created: 2017-07-12T17:27:11.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-05-05T07:47:56.000Z (about 6 years ago)
- Last Synced: 2025-03-31T06:41:25.359Z (about 1 year ago)
- Topics: hacktoberfest, json, jvm, kotlin, mustache, pattern-matching, test
- Language: Java
- Homepage:
- Size: 382 KB
- Stars: 6
- Watchers: 5
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

## Matching JSON expressively
[](https://travis-ci.org/qaware/majx) [](https://coveralls.io/github/qaware/majx?branch=master) []() [ ](https://bintray.com/qaware-oss/maven/majx/_latestVersion)
Majx is a test library for the JVM written in Kotlin that verifies that a JSON document fulfils your expectations regarding
structure and values.
You provide two JSON documents
1. The **actual** JSON that should be tested
2. The **pattern** JSON that describes your expectations
We try to let you express almost all of your expectations in a flexible, readable
and concise way within the **pattern**. To achieve this, the **pattern** may contain magic
values, that are treated in a certain way.
# Usage
Obtain a reference to the **actual** and **pattern** JSONs as `String`
or jackson's [`JsonNode`](https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/JsonNode.html)
and pass them to
- one of the static methods that the class `Majx` provides which throw `AssertionError`s
- or use the hamcrest matcher `matchesJson` from class `IsMatchingJson`
```
import static de.qaware.majx.Majx.assertJsonMatches;
import static de.qaware.majx.hamcrest.IsMatchingJson.matchesJson;
String actual = "{ \"greeting\" : \"Hello, World!\", \"id\" : 12 }";
String pattern = "{ \"greeting\" : \"Hello, World!\", \"id\" : \"...\" }";
assertJsonMatches(pattern, actual);
assertThat(actual, matchesJson(pattern); // or use the hamcrest matcher
```
This test would succeed for any value for the `"id"` property inside the **actual** JSON
since its expected value is the magic `"..."`-wildcard value in the **pattern**
(See [Ignoring values](https://github.com/qaware/majx/wiki/Ignoring-values)).
Details on all available features can be found in the wiki:
* [Matching properties and values exactly](https://github.com/qaware/majx/wiki/Matching-properties-and-values-exactly)
* [Partial object matching](https://github.com/qaware/majx/wiki/Partial-object-matching)
* [Partial array matching](https://github.com/qaware/majx/wiki/Partial-array-matching)
* [Ignoring values](https://github.com/qaware/majx/wiki/Ignoring-values)
* [Programmatic expectations](https://github.com/qaware/majx/wiki/Programmatic-expectations) through mustache expressions
## Dependencies
The JARs are available via [Maven Central](https://mvnrepository.com/artifact/de.qaware.majx/majx)
and [JCenter](https://bintray.com/qaware-oss/maven/majx).
If you are using Maven to build your project, add the following to the `pom.xml` file.
```XML
de.qaware.majx
majx
1.3.1
test
```
In case you are using Gradle to build your project, add the following to the `build.gradle` file:
```groovy
repositories {
jcenter()
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/de.qaware.majx/majx
testImplementation group: 'de.qaware.majx', name: 'majx', version: '1.3.1'
}
```
# Related Work
* Hamcrest matchers for JSON documents, https://github.com/hertzsprung/hamcrest-json
* Write JSON unit tests in less code, https://github.com/skyscreamer/JSONassert
* Compare JSON in your Unit Tests, https://github.com/lukas-krecan/JsonUnit
# Development Notes
See [Development Notes in the Wiki](https://github.com/qaware/majx/wiki/Development-Notes).
# Maintainer
Claudius Boettcher, .
# License
This software is provided under the MIT open source license, read the `LICENSE` file for details.