An open API service indexing awesome lists of open source software.

https://github.com/pwall567/kjson-test

Library for testing Kotlin JSON applications
https://github.com/pwall567/kjson-test

json kotlin unit-testing

Last synced: 4 months ago
JSON representation

Library for testing Kotlin JSON applications

Awesome Lists containing this project

README

          

# kjson-test

[![Build Status](https://github.com/pwall567/kjson-test/actions/workflows/build.yml/badge.svg)](https://github.com/pwall567/kjson-test/actions/workflows/build.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Kotlin](https://img.shields.io/static/v1?label=Kotlin&message=v2.0.21&color=7f52ff&logo=kotlin&logoColor=7f52ff)](https://github.com/JetBrains/kotlin/releases/tag/v2.0.21)
[![Maven Central](https://img.shields.io/maven-central/v/io.kjson/kjson-test?label=Maven%20Central)](https://central.sonatype.com/artifact/io.kjson/kjson-test)

Library for testing Kotlin JSON applications

This library provides a convenient means of testing applications that produce JSON results.
It uses a Kotlin DSL to describe the expected JSON values, and produces helpful messages in the case of errors.

## Contents

- [Quick Start](#quick-start)
- [User Guide](#user-guide)
- [Dependency Specification](#dependency-specification)

Or for a fuller description of the functions available, see the [`kjson-test` Reference Guide](REFERENCE.md).

## Quick Start

### Example

In a test class, the result of an operation (possibly an external client call) may be tested using `shouldMatchJSON`:
```kotlin
@Test fun `should produce correct JSON name data`() {
val json = functionUnderTest()
// json is (for example):
// {"data":[{"id":1234,"surname":"Walker","givenNames":["Richard"]}]}
json shouldMatchJSON {
property("data") {
item(0) {
property("id", 1234)
property("surname", "Walker")
property("givenNames") {
count(1)
item(0, "Richard")
}
propertyAbsent("birthDate")
}
}
}
}
```

### The Problem

Many Kotlin applications return their result in the form of a JSON object, or in some cases, as an array of JSON
objects.
When we attempt to test these applications, we run into a simple problem – there may be many valid JSON
representations of the same object.
The properties of a JSON object do not have a pre-determined order, and given a valid set of properties, any sequence of
those properties is equally valid.

Also, the JSON specification allows for whitespace to be added at many points in the JSON string, without affecting the
meaning of the content, and some primitive values may be output differently by different source systems:

- `4000` is the same number as `4e3` (see [Floating Point](#floating-point) below)
- `"Hello\n"` is the same string as `"Hello\u000A"`
- `"2025-06-22T12:00:00Z"` is the same `OffsetDateTime` as `"2025-06-22T12:00:00.000+00:00"`

All of this means that it is not possible to test the results of a function returning JSON simply by performing a string
comparison on the output.
We need a means of checking the data content of the JSON, regardless of the formatting.

Many developers avoid the problem by deserializing the JSON into the internal form of the object for comparison, but
that is not always a useful option.
And there are libraries that will check an arbitrary JSON string, but they are mostly written in and for Java, and they
do not take advantage of the capabilities of Kotlin.

### The Solution

The `kjson-test` library allows the expected content of a JSON object to be described in a simple, intuitive form,
allowing functions returning JSON to be tested easily.

In many cases, the expected values will be known precisely, and the basic forms of comparison make a good starting
point:
```kotlin
property("id", 1234)
property("surname", "Walker")
```
This checks that the current node is an object with a property named “`id`” having the value 1234, and
another property “`surname`” with the value “`Walker`”.

Nested objects may be checked by:
```kotlin
property("details") {
// tests on the nested object
}
```

And array items can similarly be checked, either as primitive values or as nested objects or arrays.

The `kjson-test` library makes testing simple cases easy and clear, and at the same time provides functionality to
meet very broad and complex testing requirements.

## User Guide

First, some terminology:
- a JSON **object** is the form of JSON structure enclosed in curly braces (` { } `) and containing a set of name-value
pairs
- these pairs are called **properties**, and each has a property name, and a value which may be a primitive value or a
nested object or array
- a JSON **array** is an ordered collection of values (possibly nested objects or arrays) enclosed in square brackets
(` [ ] `)
- an **array item** is a member of an array
- a JSON **value** is any of the possible JSON data types: a string, a number, a boolean, the value “`null`”
or a nested object or array

A function or service returning a JSON result will usually return an object or an array of objects, but according to the
JSON specification the string representation of any JSON value is a valid JSON string.
The `kjson-test` library provides facilities for testing all forms of JSON results.

### Invoking the Tests

The set of tests on a JSON string is introduced by a call to the [`shouldMatchJson`](REFERENCE.md#shouldmatchjson)
function.
This parses the JSON into an internal form and then performs the tests in the supplied lambda.
```kotlin
stringToBeTested shouldMatchJSON {
// tests...
}
```

An earlier form (still supported) uses a call to the [`expectJSON`](REFERENCE.md#expectjson) function.
The effect is the same, but the `shouldMatchJSON` syntax will probably be preferred by most users.
```kotlin
expectJSON(stringToBeTested) {
// tests...
}
```

If any of the tests fail, an `AssertionError` will be thrown with a detailed error message, usually including expected
and actual values.
The message will in most cases be prefixed by the location in the JSON of the failing test in
[JSON Pointer](https://tools.ietf.org/html/rfc6901) form.

For example:
```text
/data/0/surname: JSON value doesn't match - expected "Wilson", was "Walker"
```

### Testing JSON Properties

The [`property`](REFERENCE.md#property) function declares a test (or a group of tests) to be performed on a property of
an object.
There are several forms of the `property` function; they all take a property name (`String`) as the first parameter, and
an expected value or a lambda as the second.
The convention in Kotlin is that when an inline lambda is supplied as the last parameter to a function call, it should
be written after the closing parenthesis, as shown in the last of the examples below.

Some examples:
```kotlin
property("name", "William")
property("id", 12345678)
property("open", true)
property("ref", isUUID) // named lambda
property("details") { // inline lambda
// tests on nested object or array
}
```

The `property` call with nested tests may be made more specific, to indicate whether a nested object or an array is
expected:
```kotlin
propertyIsObject("details") {
// tests on nested object
}
propertyIsArray("list") {
// tests on nested array
}
```
These functions confirm that the property is of the correct type before applying the nested tests.
This is not strictly necessary, since the first `property` test within the nested block will check that the current node
is an object, but it does have documentation value.

And if the property is an array, the array size may be checked in the same operation:
```kotlin
propertyIsArray("list", size = 2) {
// tests on nested array
}
```

For both `propertyIsObject` and `propertyIsArray` the lambda with nested tests is optional, so they may be used to test
that a property is an object or an array respectively, with no testing of the contents.

### Testing Array Items

The [`item`](REFERENCE.md#item) function declares a test (or a group of tests) to be performed on an item of an array.
As with `property`, there are several forms of the `item` function, each taking an index (`Int`) as the first parameter,
and an expected value or a lambda as the second.

Some examples:
```kotlin
item(0, "bear")
item(27, -1)
item(1, true)
item(4) {
// tests on nested object or array
}
```

As with property tests, if the array item is expected to be an object or an array, additional functions are available:
```kotlin
itemIsObject(3) {
// tests on nested object
}
itemIsArray(0) {
// tests on nested array
}
itemIsArray(0, size = 2) {
// tests on nested array
}
```

Where the array contains only primitive values, a [shorthand form](REFERENCE.md#items) is available:
```kotlin
items("Tom", "Dick", "Harry")
```
This is equivalent to:
```kotlin
count(3)
item(0, "Tom")
item(1, "Dick")
item(2, "Harry")
```

In the case of a JSON array representing an unordered set, the [`anyItem`](REFERENCE.md#anyitem) function tests whether
any item in an array matches the specified test.
For example, an authorisation function may return a set of permissions, but the order of the entries is immaterial:
```kotlin
propertyIsArray("permissions", size = 2) {
anyItem("customer:read")
anyItem("product:read")
}
```
(See also the section on [Exhaustive Checks](#exhaustive-checks) below.)

And again, if the array item is expected to be an object or an array, additional functions are available:
```kotlin
anyItemIsObject {
// tests on nested object
}
anyItemIsArray {
// tests on nested array
}
anyItemIsArray(size = 2) {
// tests on nested array
}
```

### Testing Primitive JSON Values

In rare cases a JSON string consists of a primitive value (string, number, boolean or `null`).
The [`value`](REFERENCE.md#value) function allows tests to be applied to the single primitive value, and it takes a
single parameter, the expected value or lambda.

Some examples:
```kotlin
value("success")
value(0)
value(false)
value(null)
value(isUUID)
```

The `value` function can also be useful when more than one test is to be applied to a property or array item, for
example:
```kotlin
property("name") {
value(isNonBlankString)
value(length(1..30))
}
```

### Named Tests

The lambda parameter of the `property`, `item` or `value` tests normally takes the form of a set of tests to be applied
to a nested object or array, but it can also specify a named lambda, as in the following examples:
```kotlin
property("account", isInteger)
item(0, isString)
property("id", isUUID)
property("created", isOffsetDateTime)
```

These functions test that a property or item meets a particular requirement, without specifying the exact value.
In the above tests, the property “`account`” is checked to have an integer value, item 0 of an array
contains a string, property “`id`” contains a string formatted as a UUID and property
“`created`” contains a string following the pattern of the `OffsetDateTime` class.

See the [Reference](REFERENCE.md) document for a full list of these [Named Lambdas](REFERENCE.md#named-lambdas).

### Floating Point

Floating point numbers are those with a decimal point, or using the scientific notation (_e.g._ `1.5e20`).
Many decimal floating point numbers can not be represented with complete accuracy in a binary floating point system, so
the `kjson-test` library converts all such numbers to `BigDecimal`.
This means that tests on floating point numbers must use `BigDecimal`, or `ClosedRange`, or
`Collection`.

If a comparison using a `BigDecimal` is performed against an `Int` or a `Long`, the value will be “widened”
to `BigDecimal` before the test is performed.

One unusual feature of the `BigDecimal` class is that the `equals` comparison requires that both the value and the scale
of the number must be equal, but this library uses `compareTo` to compare the values regardless of scale.
If it is important to confirm that a certain number of decimal digits are present (for example, in a money value), the
[`scale`](REFERENCE.md#scale) function may be used to test the number of digits after the decimal point.
An `Int` or a `Long` will always be considered as having zero decimal places.

### Dates and Times

The `java.time.XXXX` classes will accept dates and times formatted in different ways, but this can cause problems for
comparisons.
For example, the UTC+0 timezone may be represented by `+00:00` or by the letter `Z`, and fractional seconds may be
truncated if not significant.

The `kjson-test` library includes comparisons using the internal form of the date/time objects, so formatting
differences will not cause tests to fail.
```kotlin
property("created", OffsetDateTime.parse("2025-06-25T15:30:00Z"))
```
The above test will succeed even if the JSON string contains `"2025-06-25T15:30:00.000-00:00"` or any other equally
valid representation of the same `OffsetDateTime`.

If it is important to check the exact format of the string, the comparison may be made using a `String` value.

### Custom Deserialization

This library looks only at the input JSON, and does not take into account any custom deserializations that may be
applied to the JSON when it is used in normal circumstances.

For example, custom name annotations are sometimes used to specify the name to be used as the JSON property name if it
differs from the internal field name; in such cases the external JSON name must be used in the tests, not the field
name.

### Check for `null`

Checking a value for `null`, _e.g._ `property("note", null)` will check that the named property **is present** in the
JSON object and has the value `null`.
Also, for convenience, a named lambda `isNull` will do the same thing.

In most cases, the fact that a property is not present in the JSON may be taken as being equivalent to the property
being present with a `null` value.
If it is important to distinguish between a `null` property and an omitted property, there are functions which test
specifically for the presence or absence of a property:

| To test that a property... | Use |
|----------------------------------------------|---------------------------------------------------------------------|
| ...is present and is not null | `property("name", isNonNull)` |
| ...is present and is null | `property("name", isNull)` |
| ...is present whether null or not | [`propertyPresent("name")`](REFERENCE.md#propertypresent) |
| ...is not present | [`propertyAbsent("name")`](REFERENCE.md#propertyabsent) |
| ...is not present, or is present and is null | [`propertyAbsentOrNull("name")`](REFERENCE.md#propertyabsentornull) |

Instead of checking all of the properties that are expected not to be present using
[`propertyAbsent("name")`](REFERENCE.md#propertyabsent) or
[`propertyAbsentOrNull("name")`](REFERENCE.md#propertyabsentornull), it may often be simpler to check the properties
that **are** present, and then use the [`count`](REFERENCE.md#count) function to check that only that number of
properties are present.

### Check for Member of Collection

You can check a value as being a member of a `Collection`.
For example:
```kotlin
property("quality", setOf("good", "bad"))
```
This test will succeed if the value of the property is one of the members of the set.

The `Collection` must be of the appropriate type for the value being checked, and each of the functions `property`,
`item` and `value` has an overloaded version that takes a `Collection`.

### Check for Value in Range

You can also check a value as being included in a range (`IntRange`, `LongRange` or `ClosedRange`).
For example:
```kotlin
property("number", 1000..9999)
```

As with `Collection`, the range must be of the appropriate type, and each of the functions `property`, `item` and
`value` has an overloaded version that takes a range.

### Check for String Length

The [`length`](REFERENCE.md#length) function is available to check the length of a string.
If, for example, you expect a string to contain between 5 and 20 characters, but you don't need to check the exact
contents, you can specify:
```kotlin
property("name", length(5..20))
```
The length may be specified as an integer value or an `IntRange`.

### Check against `Regex`

It is also possible to check a string against a `Regex`, for example:
```kotlin
property("name", Regex("^[A-Za-z]+$"))
```

### Exhaustive Checks

To confirm that all properties of an object or all items in an array are tested, the tests may be enclosed in an
[`exhaustive`](REFERENCE.md#exhaustive) block, for example:
```kotlin
propertyIsObject("address") {
exhaustive {
property("number", 27)
property("streetName", "23rd")
property("streetType", "St")
}
}
```
This will cause an error to be reported if the object contains other properties in addition to those tested.

An `exhaustive` block can also check that all items in an array have been tested.
This can be particularly useful when paired with the [`anyItem`](REFERENCE.md#anyitem) test, for example:
```kotlin
propertyIsArray("members") {
exhaustive {
anyItem("Tom")
anyItem("Dick")
anyItem("Harry")
}
}
```
This tests that the array at `members` contains the items `"Tom"`, `"Dick"` and `"Harry"` in any order, and no other
entries.

### Multiple Possibilities

You can also check for a value matching one of a number of possibilities.
The [`oneOf`](REFERENCE.md#oneof) function takes any number of lambdas (as `vararg` parameters) and executes them in
turn until it finds one that matches.
```kotlin
property("elapsedTime") {
oneOf(isDuration, isInteger)
}
```

To simplify the use of lambdas in the `oneOf` list, the [`test`](REFERENCE.md#test) function creates a lambda
representing any of the available types of test:
```kotlin
property("response") {
oneOf(test(null), test(setOf("YES", "NO")))
}
```

Of course, the full lambda syntax can be used to describe complex combinations.
In the following case, the JSON string may be either `{"data":27}` or `{"error":nnn}`, where _nnn_ is a number between
0 and 999.
```kotlin
json shouldMatchJSON {
oneOf({
property("data", 27)
},{
property("error", 0..999)
})
}
```

### Shared Tests

It is often useful to be able to share tests; for example, the same address may appear multiple times in a JSON Object.
If a function is written as an extension function on `JSONExpect`, it may be called wherever needed in the tests.
```kotlin
fun JSONExpect.checkAddress() {
property("line1", isNonBlankString)
property("line2", isNonBlankString)
property("postcode", postcodeRegex)
}
```
Then, in the tests:
```kotlin
property("address") {
checkAddress()
}
```

### Simple Tests

When the set of tests to be applied to a JSON string is very simple, it may be more convenient to express them as a
target JSON string:
```kotlin
result shouldMatchJSON """{"number":27,"name":"George"}"""
```
This is exactly equivalent to:
```kotlin
result shouldMatchJSON {
property("number", 27)
property("name", "George")
count(2)
}
```
Both the string to be tested and the target string will be parsed into an internal form, so differences in whitespace or
property order will not be significant.
And while the DSL syntax has the advantage that errors will be reported against specific lines in the set of tests, the
target string form will still give detailed error messages including the location in the JSON structure.

### Custom Tests

In any of the tests that take a lambda parameter, the lambda is not restricted to the functions provided by the library;
the full power of Kotlin is available to create tests of any complexity.

The “receiver” for the lambda is an object describing the current node in the JSON structure, and the value
is available as a `val` named
[`node`](REFERENCE.md#node-nodeasstring-nodeasint-nodeaslong-nodeasdecimal-nodeasboolean-nodeasobject-nodeasarray).
The type of `node` is `JSONValue?`; [`JSONValue`](https://github.com/pwall567/kjson-core#jsonvalue) is a sealed
interface, and the implementing classes represent the JSON data types:

- [`JSONString`](https://github.com/pwall567/kjson-core#jsonstring) – a string value
- [`JSONInt`](https://github.com/pwall567/kjson-core#jsonint) – a number value that fits in a 32-bit signed
integer
- [`JSONLong`](https://github.com/pwall567/kjson-core#jsonlong) – a number value that fits in a 64-bit signed
integer
- [`JSONDecimal`](https://github.com/pwall567/kjson-core#jsondecimal) – any number value, including non-integer
(uses `BigDecimal` internally)
- [`JSONBoolean`](https://github.com/pwall567/kjson-core#jsonboolean) – a boolean value
- [`JSONArray`](https://github.com/pwall567/kjson-core#jsonarray) – an array
- [`JSONObject`](https://github.com/pwall567/kjson-core#jsonobject) – an object

There are also conversion functions, each of which takes the form of a `val` with a custom accessor.
These accessors either return the node in the form requested, or throw an `AssertionError` with a detailed error
message.

| Accessor | Type |
|-----------------|-----------------------------------------------------|
| `nodeAsString` | `String` |
| `nodeAsInt` | `Int` |
| `nodeAsLong` | `Long` |
| `nodeAsDecimal` | `BigDecimal` |
| `nodeAsBoolean` | `Boolean` |
| `nodeAsArray` | `JSONArray` (implements `List`) |
| `nodeAsObject` | `JSONObject` (implements `Map`) |

To report errors, the [`error`](REFERENCE.md#error) function will create an `AssertionError` with the message provided,
prepending the JSON pointer location for the current node in the JSON.
The [`showNode`](REFERENCE.md#shownode) function can be used to display the actual node value in the error message.

Example:
```kotlin
jsonString shouldMatchJSON {
property("abc") {
if (nodeAsInt.rem(3) != 0)
error("Value not divisible by 3 - ${showNode()}")
}
}
```

### Spring Framework

Many users will wish to use `kjson-test` in conjunction with the
[Spring Framework](https://spring.io/projects/spring-framework).
A suggestion for simplifying some forms of Spring testing is included in the [Spring and `kjson-test`](SPRING.md) guide.

## Dependency Specification

The latest version of the library is 5.1, and it may be obtained from the Maven Central repository.
(The following dependency declarations assume that the library will be included for test purposes; this is
expected to be its principal use.)

### Maven
```xml

io.kjson
kjson-test
5.1
test

```
### Gradle
```groovy
testImplementation 'io.kjson:kjson-test:5.1'
```
### Gradle (kts)
```kotlin
testImplementation("io.kjson:kjson-test:5.1")
```

Peter Wall

2025-06-24