assertj-json/README.md

48 lines
1.5 KiB
Markdown
Raw Normal View History

2024-10-24 19:07:41 +00:00
# AssertJ - fluent assertions for json
This library provides helper methods for asserting JSON strings with AssertJ.
2024-11-15 14:25:10 +00:00
Syntax Examples:
```java
// Assert that a root property is equal to a string
JsonAssert.assertThat("myprop").isEqualTo("foo");
// Assert that the traversed path exists and the property is larger than two
JsonAssert.assertThat(JSON).path("a", "b", "c").isEqualTo(2);
// OR
JsonAssert.assertThat(JSON).field("a").field("b").field("c").isEqualTo(2);
// Cast property before asserting
JsonAssert.assertThat(JSON).field("bigdecimal").asBigDecimal().isEqualTo(BigDecimal.valueOf(1234.0));
// Match by regular expression (myprop:"foobar")
JsonAssert.assertThat(JSON).field("myprop").matches("foo.*")
// Assert first property in Object matches
JsonAssert.assertThat(JSON).firstField("lastProp").isEqualTo(1);
// Assert last property in Object matches
JsonAssert.assertThat(JSON).lastField("lastProp").isEqualTo(3);
// Assert nth property in Object matches
JsonAssert.assertThat(JSON).nthField("secondProp").isEqualTo(2);
// Assert root typed array "[1,2,3]"
JsonAssert.assertThat(JSON).asArray(Integer.class).first().isEqualTo(1);
// Assert root array with objects "[{ prop: 1}]"
JsonAssert.assertThat(JSON).asArray().first().field("prop").isEqualTo(1);
// Assert matrix [[1,2],[3,4]]
assertThat(JSON).asArray().last().asArray().first().asInteger().isEqualTo(3);
```
For more examples see the unit tests in [unit tests](src/test/java/com/devsoap/json).
2024-10-24 19:07:41 +00:00
Licensed under [CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0/).