From ae91c8460fd056afb7984949f2662e626f0653d4 Mon Sep 17 00:00:00 2001 From: Mihhail Lapushkin Date: Tue, 4 Jul 2017 11:07:42 +0300 Subject: [PATCH 1/2] Fix JsonContentAssert type safety warnings Previously, JsonContentAssert returns AbstractMapAssert from extractingJsonPathMapValue. This could lead to type safety warnings when calling one of the assert's methods with a generic varargs parameter such as contains(Entry...). This commit replaces the use of both AbstractMapAssert and AbstractListAssert with MapAssert and ListAssert respectively. These classes use final methods and @SafeVargs args to prevent the above-described problem from occurring. See gh-9675 --- .../boot/test/json/JsonContentAssert.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java index 6e063ac079..49eb880756 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java @@ -26,12 +26,11 @@ import com.jayway.jsonpath.JsonPath; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractBooleanAssert; import org.assertj.core.api.AbstractCharSequenceAssert; -import org.assertj.core.api.AbstractListAssert; -import org.assertj.core.api.AbstractMapAssert; import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.Assert; import org.assertj.core.api.Assertions; -import org.assertj.core.api.ObjectAssert; +import org.assertj.core.api.ListAssert; +import org.assertj.core.api.MapAssert; import org.skyscreamer.jsonassert.JSONCompare; import org.skyscreamer.jsonassert.JSONCompareMode; import org.skyscreamer.jsonassert.JSONCompareResult; @@ -947,11 +946,12 @@ public class JsonContentAssert extends AbstractAssert element type * @return a new assertion object whose object under test is the extracted item * @throws AssertionError if the path is not valid or does not result in an array */ @SuppressWarnings("unchecked") - public AbstractListAssert> extractingJsonPathArrayValue( + public ListAssert extractingJsonPathArrayValue( CharSequence expression, Object... args) { return Assertions.assertThat( extractingJsonPathValue(expression, args, List.class, "an array")); @@ -961,12 +961,14 @@ public class JsonContentAssert extends AbstractAssert key type + * @param value type * formatting specifiers defined in {@link String#format(String, Object...)} * @return a new assertion object whose object under test is the extracted item * @throws AssertionError if the path is not valid or does not result in a map */ @SuppressWarnings("unchecked") - public AbstractMapAssert extractingJsonPathMapValue( + public MapAssert extractingJsonPathMapValue( CharSequence expression, Object... args) { return Assertions.assertThat( extractingJsonPathValue(expression, args, Map.class, "a map")); From f6727e786a2c50fd54d7387e1f4e94f78dc0dc3a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 21 Jul 2017 15:10:23 +0100 Subject: [PATCH 2/2] Polish "Fix JsonContentAssert type safety warnings" - Format code - Update an existing test to use the contains method that would previously have triggered a type safety warning Closes gh-9675 --- .../boot/test/json/JsonContentAssert.java | 12 ++++++------ .../boot/test/json/JsonContentAssertTests.java | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java index 49eb880756..4d83e38199 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java @@ -951,8 +951,8 @@ public class JsonContentAssert extends AbstractAssert ListAssert extractingJsonPathArrayValue( - CharSequence expression, Object... args) { + public ListAssert extractingJsonPathArrayValue(CharSequence expression, + Object... args) { return Assertions.assertThat( extractingJsonPathValue(expression, args, List.class, "an array")); } @@ -962,14 +962,14 @@ public class JsonContentAssert extends AbstractAssert key type - * @param value type - * formatting specifiers defined in {@link String#format(String, Object...)} + * @param value type formatting specifiers defined in + * {@link String#format(String, Object...)} * @return a new assertion object whose object under test is the extracted item * @throws AssertionError if the path is not valid or does not result in a map */ @SuppressWarnings("unchecked") - public MapAssert extractingJsonPathMapValue( - CharSequence expression, Object... args) { + public MapAssert extractingJsonPathMapValue(CharSequence expression, + Object... args) { return Assertions.assertThat( extractingJsonPathValue(expression, args, Map.class, "a map")); } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonContentAssertTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonContentAssertTests.java index c8226051af..b3e185795e 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonContentAssertTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonContentAssertTests.java @@ -37,6 +37,7 @@ import org.springframework.test.util.JsonPathExpectationsHelper; import org.springframework.util.FileCopyUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; /** * Tests for {@link JsonContentAssert}. Some tests here are based on Spring Framework @@ -1252,7 +1253,7 @@ public class JsonContentAssertTests { @Test public void extractingJsonPathMapValue() throws Exception { assertThat(forJson(TYPES)).extractingJsonPathMapValue("@.colorMap") - .containsEntry("red", "rojo"); + .contains(entry("red", "rojo")); } @Test