JUnit JSON Params

JUnit 5 JSON Parameterized Tests library

License

License

Categories

Categories

JUnit Unit Testing Net JSON Data
GroupId

GroupId

net.joshka
ArtifactId

ArtifactId

junit-json-params
Last Version

Last Version

5.6.2-r0
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

JUnit JSON Params
JUnit 5 JSON Parameterized Tests library
Project URL

Project URL

http://www.joshka.net/junit-json-params/
Source Code Management

Source Code Management

https://github.com/joshka/junit-json-params/

Download junit-json-params

Dependencies

compile (3)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar
org.junit.jupiter : junit-jupiter-params jar
javax.json : javax.json-api jar 1.1.4

Project Modules

There are no modules declared in this project.

junit-json-params

A Junit 5 library to provide annotations that load data from JSON Strings or files in parameterized tests.

Project Info

Project site

Build Status Maven Central Javadocs Quality Gate Coverage License Dependabot Status

Installation

Apache Maven

<dependency>
    <groupId>net.joshka</groupId>
    <artifactId>junit-json-params</artifactId>
    <version>5.6.2-r1</version>
</dependency>

Gradle

compile 'net.joshka:junit-json-params:5.6.2-r1'

Examples

@JsonSource

@JsonSource allows you to specify argument lists as JSON strings.

See JsonArgumentsProviderTest

import net.joshka.junit.json.params.JsonSource;

class JsonArgumentsProviderTest {
    /**
     * When passed <code>{"key":"value"}</code>, is executed a single time
     * @param object the parsed JsonObject
     */
    @ParameterizedTest
    @JsonSource("{\"key\":\"value\"}")
    @DisplayName("provides a single object")
    void singleObject(JsonObject object) {
        assertThat(object.getString("key")).isEqualTo("value");
    }

    /**
     * When passed <code>[{"key":"value1"},{"key","value2"}]</code>, is
     * executed once per element of the array
     * @param object the parsed JsonObject array element
     */
    @ParameterizedTest
    @JsonSource("[{\"key\":\"value1\"},{\"key\":\"value2\"}]")
    @DisplayName("provides an array of objects")
    void arrayOfObjects(JsonObject object) {
        assertThat(object.getString("key")).startsWith("value");
    }

    /**
     * When passed <code>[1, 2]</code>, is executed once per array element
     * @param number the parsed JsonNumber for each array element
     */
    @ParameterizedTest
    @JsonSource("[1,2]")
    @DisplayName("provides an array of numbers")
    void arrayOfNumbers(JsonNumber number) {
        assertThat(number.intValue()).isGreaterThan(0);
    }

    /**
     * When passed <code>["value1","value2"]</code>, is executed once per array
     * element
     * @param string the parsed JsonString for each array element
     */
    @ParameterizedTest
    @JsonSource("[\"value1\",\"value2\"]")
    @DisplayName("provides an array of strings")
    void arrayOfStrings(JsonString string) {
        assertThat(string.getString()).startsWith("value");
    }

    /**
     * When passed <code>{'key':'value'}</code>, is executed a single time.
     * This simplifies writing inline JSON strings
     * @param object the parsed JsonObject
     */
    @ParameterizedTest
    @JsonSource("{'key':'value'}")
    @DisplayName("handles simplified json")
    void simplifiedJson(JsonObject object) {
        assertThat(object.getString("key")).isEqualTo("value");
    }
}

@JsonFileSource

@JsonFileSource lets you use JSON files from the classpath. It supports single objects and arrays of objects and JSON primitives (numbers and strings).

See JsonFileArgumentsProviderTest

import net.joshka.junit.json.params.JsonFileSource;

class JsonFileArgumentsProviderTest {
    /**
     * When passed <code>{"key":"value"}</code>, is executed a single time
     * @param object the parsed JsonObject
     */
    @ParameterizedTest
    @JsonFileSource(resources = "/single-object.json")
    @DisplayName("provides a single object")
    void singleObject(JsonObject object) {
        assertThat(object.getString("key")).isEqualTo("value");
    }

    /**
     * When passed <code>[{"key":"value1"},{"key","value2"}]</code>, is
     * executed once per element of the array
     * @param object the parsed JsonObject array element
     */
    @ParameterizedTest
    @JsonFileSource(resources = "/array-of-objects.json")
    @DisplayName("provides an array of objects")
    void arrayOfObjects(JsonObject object) {
        assertThat(object.getString("key")).startsWith("value");
    }

    /**
     * When passed <code>[1, 2]</code>, is executed once per array element
     * @param number the parsed JsonNumber for each array element
     */
    @ParameterizedTest
    @JsonFileSource(resources = "/array-of-numbers.json")
    @DisplayName("provides an array of numbers")
    void arrayOfNumbers(JsonNumber number) {
        assertThat(number.intValue()).isGreaterThan(0);
    }

    /**
     * When passed <code>["value1","value2"]</code>, is executed once per array
     * element
     * @param string the parsed JsonString for each array element
     */
    @ParameterizedTest
    @JsonFileSource(resources = "/array-of-strings.json")
    @DisplayName("provides an array of strings")
    void arrayOfStrings(JsonString string) {
        assertThat(string.getString()).startsWith("value");
    }
}

License

Copyright 2019 Joshua McKinney

Code is under the Apache License 2.0

Versions

Version
5.6.2-r0
5.5.2-r0
5.5.1-r0
5.4.2-r0
5.4.0-r0
1.3.2
1.3.1-1
1.3.1
1.1.0
1.0.0