diff --git a/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssertions.java b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssertions.java index 12aeb256..b393c318 100644 --- a/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssertions.java +++ b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssertions.java @@ -21,6 +21,7 @@ import net.javacrumbs.jsonunit.core.internal.JsonUtils; import org.assertj.core.api.AssertFactory; import org.assertj.core.api.Assertions; +import org.assertj.core.api.InstanceOfAssertFactory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -76,10 +77,35 @@ public static Object value(Object input) { return new ExpectedNode(JsonUtils.wrapDeserializedObject(input)); } + /** + * Returns AssertFactory allowing to move from standard AssertJ to JsonUnit. For example: + * + * assertThat(mvc.get().uri("/sample")) + * .hasStatusOk() + * .bodyJson() + * .convertTo(jsonUnitAssert()) + * .inPath("result.array") <-- JsonUnit assert + * .isArray() + * .containsExactly(1, 2, 3); + * + */ public static AssertFactory jsonUnitAssert() { return new JsonUnitAssertFactory(); } + /** + * Allows to move from standard AssertJ asserts to JsonUnit. For example + * + * assertThat(resp) + * .hasFieldOrPropertyWithValue("trackingId", "abcd-0001") //<- Assertj API + * .extracting("json").asInstanceOf(jsonUnitJson()) + * .isObject().containsEntry("foo", "bar"); // <- JsonUnit API + * + */ + public static InstanceOfAssertFactory jsonUnitJson() { + return new InstanceOfAssertFactory<>(Object.class, jsonUnitAssert()); + } + @FunctionalInterface public interface JsonAssertionCallback { void doAssert(@NotNull ConfigurableJsonAssert assertion); diff --git a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java index 192ad584..d1271e5a 100644 --- a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java +++ b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java @@ -21,6 +21,7 @@ import static java.util.Collections.singletonMap; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json; +import static net.javacrumbs.jsonunit.assertj.JsonAssertions.jsonUnitJson; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.value; import static net.javacrumbs.jsonunit.core.ConfigurationWhen.path; import static net.javacrumbs.jsonunit.core.ConfigurationWhen.paths; @@ -2239,6 +2240,19 @@ void shouldIgnoreExtraArrayItemsInSpecificPath() { .isEqualTo("{\"a\":[1,2]}"); } + @Test + void shouldUseAsInstanceOfToMoveToJsonUnit() { + record DummyResponse(String trackingId, String json) {} + DummyResponse resp = new DummyResponse("abcd-0001", "{ \"foo\": \"bar\" }"); + + assertThat(resp) + .hasFieldOrPropertyWithValue("trackingId", "abcd-0001") // <- Assertj API + .extracting("json") + .asInstanceOf(jsonUnitJson()) + .isObject() + .containsEntry("foo", "bar"); // <- JsonUnit API + } + @Test void shouldNotIgnoreExtraArrayItemsWhenNotSpecified() { assertThatThrownBy(() -> assertThatJson("{\"a\":[1,2,3],\"b\":[1,2,3]}")