mateuszjarzyna / assertj-json

A set of AssertJ assertions to validate JSON.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AssertJ Json

Build Status Maven Central Javadoc codecov.io Codacy Badge

A set of AssertJ assertions to validate JSON.

JsonPath

Use JsonPathAssert to extract values using JsonPath and validate as a Java string, integer or list.

Example:

DocumentContext ctx = JsonPath.parse("{\"value\":\"a text value\"}");

assertThat(ctx).jsonPathAsString("$.value").contains("text").endsWith("value");

Runtime configuration

JsonPathAssert uses JayWay JsonPath under the covers. In order to execute more complex extractions like array of strings to List<String> you need to have Jackson or Gson in your classpath and configure JsonPath to use one of them.

One option is to execute the following snipped of code in a @BeforeClass method in your test class.

Configuration.setDefaults(new Configuration.Defaults() {
    private final JsonProvider jsonProvider = new JacksonJsonProvider();
    private final MappingProvider mappingProvider = new JacksonMappingProvider();

    @Override
    public JsonProvider jsonProvider() {
        return jsonProvider;
    }

    @Override
    public MappingProvider mappingProvider() {
        return mappingProvider;
    }

    @Override
    public Set<Option> options() {
        return EnumSet.noneOf(Option.class);
    }
});

About

A set of AssertJ assertions to validate JSON.

License:MIT License


Languages

Language:Java 100.0%