Skip to content

AssertJ JSON

Release Build and Test License

AssertJ JSON adds fluent assertions tailored for JSON payloads, JSON files, and JSON tree values.

Goals

  • Assert JSON structure and values using the fluent AssertJ style
  • Work seamlessly with JsonNode from Jackson
  • Support JSON files as inputs
  • Remain easy to consume from Maven and Gradle

Installation (JitPack)

AssertJ JSON is currently available via JitPack during its beta period.

Maven

  1. Add the JitPack repository to your pom.xml:
<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>
  1. Add the dependency:
<dependency>
  <groupId>com.github.archyoshi</groupId>
  <artifactId>assertj-json</artifactId>
  <version>v0.1.2</version>
  <scope>test</scope>
</dependency>

Note: To consume the latest development snapshots, you can use <version>develop-SNAPSHOT</version> or a specific commit hash.

Gradle

Groovy DSL (build.gradle)

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

dependencies {
    testImplementation 'com.github.archyoshi:assertj-json:v0.1.2'
}

Kotlin DSL (build.gradle.kts)

repositories {
    mavenCentral()
    maven("https://jitpack.io")
}

dependencies {
    testImplementation("com.github.archyoshi:assertj-json:v0.1.2")
}

Usage

import static com.archyoshi.assertj.json.JsonAssertions.assertThat;

String json = "{ \"name\": \"Vegeta\", \"age\": 30, \"active\": true }";

assertThat(json)
  .hasField("name")
  .hasValueForField("Vegeta", "name")
  .hasField("age")
  .hasValueForField(30, "age");
import static com.archyoshi.assertj.json.JsonAssertions.assertThat;

Path file = Path.of("src/test/resources/example.json");

assertThat(file).hasSameContentAs("""
    {
      "name": "Vegeta"
    }
    """);

assertThat(file).hasSameFieldsAs(Path.of("src/test/resources/expected.json"));

assertThat(file).partiallyContains("""
    {
      "active": true
    }
    """);

Building

./mvnw clean verify

Author

License

This project is licensed under the Apache License 2.0.

About

AssertJ JSON adds fluent assertions tailored for JSON payloads, JSON files, and JSON tree values.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages