Skip to content

Commit

Permalink
Support for InstanceOfAssertFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Nov 7, 2024
1 parent 0b4e62f commit b7cef54
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
* <code>
* assertThat(mvc.get().uri("/sample"))
* .hasStatusOk()
* .bodyJson()
* .convertTo(jsonUnitAssert())
* .inPath("result.array") <-- JsonUnit assert
* .isArray()
* .containsExactly(1, 2, 3);
* </code>
*/
public static AssertFactory<Object, ConfigurableJsonAssert> jsonUnitAssert() {
return new JsonUnitAssertFactory();
}

/**
* Allows to move from standard AssertJ asserts to JsonUnit. For example
* <code>
* assertThat(resp)
* .hasFieldOrPropertyWithValue("trackingId", "abcd-0001") //<- Assertj API
* .extracting("json").asInstanceOf(jsonUnitJson())
* .isObject().containsEntry("foo", "bar"); // <- JsonUnit API
* </code>
*/
public static InstanceOfAssertFactory<Object, ConfigurableJsonAssert> jsonUnitJson() {
return new InstanceOfAssertFactory<>(Object.class, jsonUnitAssert());
}

@FunctionalInterface
public interface JsonAssertionCallback {
void doAssert(@NotNull ConfigurableJsonAssert assertion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]}")
Expand Down

0 comments on commit b7cef54

Please sign in to comment.