Java EE 8 and JSON-P Simplify System Testing with JSON Patch

JSON-P (JSR-374) from Java EE 8 comes with JSON Patch support, which is useful for system testing.

A JSON object returned from a HTTP endpoint can be easily compared to a local representation loaded from a file:

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonPatch;
import javax.json.JsonReader;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class JSONPDiffTest {

    public void subtraction() {
        //e.g. server result
        JsonObject actual = Json.createObjectBuilder().
                add("a", 21).
                add("b", 1).

        try (JsonReader reader
                = Json.createReader(this.getClass().getResourceAsStream("/expected.json"))) {
            JsonObject expected = reader.readObject();
            JsonPatch diff = Json.createDiff(expected, actual);
            assertTrue(diff.toString(), diff.toJsonArray().isEmpty());

The code above loads the file src/test/resources/expected.json with the content:

    "a": 21,
    "b": 2

and compares it to the actual instance.

The result is:

java.lang.AssertionError: [{"op":"replace","path":"/b","value":1}]

JSON-P comes as a single maven dependency:


See you at Java EE Workshops at MUC Airport, particularly at the Java EE CI/CD, Testing and Quality workshop

NEW online workshop: Web Components training (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: and newsletter:

A book about rethinking Java EE Patterns



Posted by on February 17, 2018 at 07:10 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments