Skip to content

Commit

Permalink
Kotest in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Sep 14, 2023
1 parent dae42c0 commit 9d6e4fa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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).

## <a name="kotest"></a>Kotest assertions
JsonUnit supports [Kotest assertions](https://kotest.io/docs/assertions/assertions.html).

Import:

```xml
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-kotest</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
```

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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Different value found in node "test", expected: <2> but was: <1>.""")
@Test
fun `Should assert path`() {
assertThrows<AssertionError> {
"""{"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>.""")
}
Expand All @@ -64,7 +64,7 @@ Different value found in node "test", expected: <2> but was: <1>.""")
fun `Should assert nested`() {
assertThrows<AssertionError> {
"""{"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:
Expand Down

0 comments on commit 9d6e4fa

Please sign in to comment.