A set of AssertJ assertions to validate JSON.
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");
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);
}
});