diff --git a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java index cbe6017b..3a062ab3 100644 --- a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java +++ b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java @@ -18,6 +18,7 @@ import java.math.BigDecimal; import java.util.function.BiConsumer; import net.javacrumbs.jsonunit.core.Configuration; +import net.javacrumbs.jsonunit.core.ConfigurationWhen; import net.javacrumbs.jsonunit.core.Option; import net.javacrumbs.jsonunit.core.internal.Path; import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher; @@ -113,6 +114,17 @@ public ME when(@NotNull Option firstOption, @NotNull Option... otherOptions) { return matchers(path, configuration.withOptions(firstOption, otherOptions)); } + /** + * Adds path specific options. + * + * @see Configuration#when(ConfigurationWhen.PathsParam, ConfigurationWhen.ApplicableForPath...) + */ + @NotNull + public ME when( + @NotNull ConfigurationWhen.PathsParam object, @NotNull ConfigurationWhen.ApplicableForPath... actions) { + return matchers(path, configuration.when(object, actions)); + } + /** * Compares JSON for equality. The expected object is converted to JSON * before comparison. Ignores order of sibling nodes and whitespaces. diff --git a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java index 4ae9537a..cfe0ee98 100644 --- a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java +++ b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java @@ -16,6 +16,9 @@ package net.javacrumbs.jsonunit.spring.testit; import static java.math.BigDecimal.valueOf; +import static net.javacrumbs.jsonunit.core.ConfigurationWhen.path; +import static net.javacrumbs.jsonunit.core.ConfigurationWhen.then; +import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; import static net.javacrumbs.jsonunit.spring.JsonUnitResultMatchers.json; import static net.javacrumbs.jsonunit.spring.testit.demo.ExampleController.CORRECT_JSON; import static net.javacrumbs.jsonunit.spring.testit.demo.ExampleController.ISO_VALUE; @@ -28,7 +31,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import net.javacrumbs.jsonunit.core.Option; import net.javacrumbs.jsonunit.core.listener.Difference; import net.javacrumbs.jsonunit.core.listener.DifferenceContext; import net.javacrumbs.jsonunit.core.listener.DifferenceListener; @@ -65,6 +67,13 @@ void shouldPassIfEqualsWithProduces() throws Exception { exec("/sampleProduces").andExpect(json().isEqualTo(CORRECT_JSON)); } + @Test + void shouldAllowOptionsOnPath() throws Exception { + exec("/sampleProduces") + .andExpect(json().when(path("result.array"), then(IGNORING_ARRAY_ORDER)) + .isEqualTo(CORRECT_JSON)); + } + @Test void shouldPassIfEqualsWithIsoEncoding() throws Exception { exec("/sampleIso").andExpect(json().node("result").isEqualTo(ISO_VALUE)); @@ -234,9 +243,7 @@ void shouldSetTolerance() throws Exception { @Test void settingOptionShouldTakeEffect() throws Exception { - exec().andExpect(json().node("result.array") - .when(Option.IGNORING_ARRAY_ORDER) - .isEqualTo(new int[] {3, 2, 1})); + exec().andExpect(json().node("result.array").when(IGNORING_ARRAY_ORDER).isEqualTo(new int[] {3, 2, 1})); } @Test