From 693101ded876318a489e6347a47cb61dcb2a58be Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 23 Oct 2019 14:10:25 +0200 Subject: [PATCH] Remove unused type parameter declarations in MockMvc result matchers Prior to this commit, several of the ResultMatcher methods used in MockMvc declared unused type parameters (e.g., ). This was obviously the result of copying an existing method that actually needs the type parameter for proper casting. For example, the following in RequestResultMatchers ... public ResultMatcher attribute(String name, Object expectedValue) { // ... } ... should actually be declared without , since T is not used in the implementation or in the return type: public ResultMatcher attribute(String name, Object expectedValue) { // ... } This commit removes all unused type parameter declarations in MockMvc result matchers. Side Effects: Now that we have removed the unused type parameter declarations, users will see the following side effects if they had previously declared a type argument when invoking such methods. - Java: an "Unused type arguments for the non generic method ..." warning will be generated by the compiler, but the code will continue to work unmodified. - Kotlin: a "Type inference failed: Not enough information to infer parameter T in fun ..." compiler error will be raised, causing the code to no longer compile (see https://youtrack.jetbrains.com/issue/KT-5464). Removal of the type argument declaration will allow the code to work correctly again. Closes gh-23858 --- .../servlet/result/FlashAttributeResultMatchers.java | 6 +++--- .../test/web/servlet/result/HeaderResultMatchers.java | 2 +- .../test/web/servlet/result/ModelResultMatchers.java | 10 +++++----- .../test/web/servlet/result/RequestResultMatchers.java | 6 +++--- .../test/web/servlet/MockMvcExtensionsTests.kt | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java index 5e79c08216..41512061dc 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java @@ -54,14 +54,14 @@ public class FlashAttributeResultMatchers { /** * Assert a flash attribute's value. */ - public ResultMatcher attribute(String name, Object value) { + public ResultMatcher attribute(String name, Object value) { return result -> assertEquals("Flash attribute '" + name + "'", value, result.getFlashMap().get(name)); } /** * Assert the existence of the given flash attributes. */ - public ResultMatcher attributeExists(String... names) { + public ResultMatcher attributeExists(String... names) { return result -> { for (String name : names) { assertNotNull("Flash attribute '" + name + "' does not exist", result.getFlashMap().get(name)); @@ -72,7 +72,7 @@ public class FlashAttributeResultMatchers { /** * Assert the number of flash attributes. */ - public ResultMatcher attributeCount(int count) { + public ResultMatcher attributeCount(int count) { return result -> assertEquals("FlashMap size", count, result.getFlashMap().size()); } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java index 60203db6d4..9a764a07e2 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java @@ -65,7 +65,7 @@ public class HeaderResultMatchers { * Iterable {@link Matcher}. * @since 4.3 */ - public ResultMatcher stringValues(String name, Matcher> matcher) { + public ResultMatcher stringValues(String name, Matcher> matcher) { return result -> { List values = result.getResponse().getHeaders(name); assertThat("Response header '" + name + "'", values, matcher); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java index 507fd7b87b..b16712f3e5 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java @@ -172,7 +172,7 @@ public class ModelResultMatchers { * Assert a field error code for a model attribute using a {@link org.hamcrest.Matcher}. * @since 4.1 */ - public ResultMatcher attributeHasFieldErrorCode(String name, String fieldName, + public ResultMatcher attributeHasFieldErrorCode(String name, String fieldName, Matcher matcher) { return mvcResult -> { @@ -189,7 +189,7 @@ public class ModelResultMatchers { /** * Assert the total number of errors in the model. */ - public ResultMatcher errorCount(int expectedCount) { + public ResultMatcher errorCount(int expectedCount) { return result -> { int actualCount = getErrorCount(getModelAndView(result).getModelMap()); assertEquals("Binding/validation error count", expectedCount, actualCount); @@ -199,7 +199,7 @@ public class ModelResultMatchers { /** * Assert the model has errors. */ - public ResultMatcher hasErrors() { + public ResultMatcher hasErrors() { return result -> { int count = getErrorCount(getModelAndView(result).getModelMap()); assertTrue("Expected binding/validation errors", count != 0); @@ -209,7 +209,7 @@ public class ModelResultMatchers { /** * Assert the model has no errors. */ - public ResultMatcher hasNoErrors() { + public ResultMatcher hasNoErrors() { return result -> { ModelAndView mav = getModelAndView(result); for (Object value : mav.getModel().values()) { @@ -223,7 +223,7 @@ public class ModelResultMatchers { /** * Assert the number of model attributes. */ - public ResultMatcher size(int size) { + public ResultMatcher size(int size) { return result -> { ModelAndView mav = getModelAndView(result); int actual = 0; diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java index 55f317c7a0..a92569e3a3 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java @@ -98,7 +98,7 @@ public class RequestResultMatchers { * or {@link WebAsyncTask}. The value matched is the value returned from the * {@code Callable} or the exception raised. */ - public ResultMatcher asyncResult(Object expectedResult) { + public ResultMatcher asyncResult(Object expectedResult) { return result -> { HttpServletRequest request = result.getRequest(); assertAsyncStarted(request); @@ -120,7 +120,7 @@ public class RequestResultMatchers { /** * Assert a request attribute value. */ - public ResultMatcher attribute(String name, Object expectedValue) { + public ResultMatcher attribute(String name, Object expectedValue) { return result -> assertEquals("Request attribute '" + name + "'", expectedValue, result.getRequest().getAttribute(name)); } @@ -141,7 +141,7 @@ public class RequestResultMatchers { /** * Assert a session attribute value. */ - public ResultMatcher sessionAttribute(String name, Object value) { + public ResultMatcher sessionAttribute(String name, Object value) { return result -> { HttpSession session = result.getRequest().getSession(); Assert.state(session != null, "No HttpSession"); diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt index 98dfa3effb..2ac5accb3a 100644 --- a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt @@ -130,7 +130,7 @@ class MockMvcExtensionsTests { assertThatExceptionOfType(AssertionError::class.java).isThrownBy { content { json("""{"name":"wrong"}""") } } assertThatExceptionOfType(AssertionError::class.java).isThrownBy { jsonPath("name") { value("wrong") } } assertThatExceptionOfType(AssertionError::class.java).isThrownBy { cookie { value("name", "wrong") } } - assertThatExceptionOfType(AssertionError::class.java).isThrownBy { flash { attribute("name", "wrong") } } + assertThatExceptionOfType(AssertionError::class.java).isThrownBy { flash { attribute("name", "wrong") } } assertThatExceptionOfType(AssertionError::class.java).isThrownBy { header { stringValues("name", "wrong") } } assertThatExceptionOfType(AssertionError::class.java).isThrownBy { model { attributeExists("name", "wrong") } } assertThatExceptionOfType(AssertionError::class.java).isThrownBy { redirectedUrl("wrong/Url") }