diff --git a/README.md b/README.md index cac89b78..3e7a8738 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ JsonUnit is a library that simplifies JSON comparison in tests. * [Spring MVC assertions](#spring) * [Spring WebTestClient](#spring-web-client) * [Spring REST client assertions](#spring-client) + * [Kotest assertions](#kotest) * [Vintage APIs](#vintage) - [Features](#features) * [JsonPath support](#jsonpath) @@ -289,6 +290,55 @@ To use import For more examples see [the tests](https://github.com/lukas-krecan/JsonUnit/blob/master/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/ClientTest.java). +## Kotest assertions +JsonUnit supports [Kotest assertions](https://kotest.io/docs/assertions/assertions.html). + +Import: + +```xml + + net.javacrumbs.json-unit + json-unit-kotest + 3.1.0 + test + +``` + +And enjoy: + +```kotlin +"""{"test":1}""" should equalJson("""{"test": 1}""") + +// Provide configuration +"""{"test":1.01}""" should equalJson("""{"test":1}""", configuration { withTolerance(0.1) }) + +// Use inPath +"""{"test":1}""" inPath "test" should equalJson("1") + +// Clues with nesting +"""{"test": {"nested": 1}}""".inPath("test").asClue { + it inPath "nested" should equalJson("2") +} + +"""{"test":1}""".inPath("test").shouldBeJsonNumber() + // shouldBeJsonNumber returns BigDecimal, so we can use standard kotest assertions + // PLease note that numbers are converted to BigDecimals + .shouldBeEqualComparingTo(valueOf(1)) + +// The same for arrays generated by JsonPath +"""{"test": [{"a": "a"}, {"a": true}, {"a": null}, {"a": 4}]}""".inPath("$.test[*].a") + .shouldBeJsonArray() + .shouldContainExactly("a", true, null, valueOf(4)) + +// ... and objects +"""{"a":1, "b": true}""".shouldBeJsonObject().shouldMatchAll( + "a" to { it should beJsonNumber() }, + "b" to { it should beJsonBoolean() } +) +``` + +See the [tests](https://github.com/lukas-krecan/JsonUnit/blob/master/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt) for more examples. + # Features JsonUnit support all this features regardless of API you use. diff --git a/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt b/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt index bf49c88e..c8b5cdc1 100644 --- a/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt +++ b/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt @@ -55,7 +55,7 @@ Different value found in node "test", expected: <2> but was: <1>.""") @Test fun `Should assert path`() { assertThrows { - """{"test":1}""" inPath "test" should equalJson("""2""") + """{"test":1}""" inPath "test" should equalJson("2") }.shouldHaveMessage("""JSON documents are different: Different value found in node "test", expected: <2> but was: <1>.""") } @@ -64,7 +64,7 @@ Different value found in node "test", expected: <2> but was: <1>.""") fun `Should assert nested`() { assertThrows { """{"test": {"nested": 1}}""".inPath("test").asClue { - it inPath "nested" should equalJson("""2""") + it inPath "nested" should equalJson("2") } }.shouldHaveMessage("""JSON in path "test" JSON documents are different: