Browse Source

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., <T>). 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 <T> ResultMatcher attribute(String name, Object expectedValue) {
    // ...
}

... should actually be declared without <T>, 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
pull/23863/head
Sam Brannen 5 years ago
parent
commit
693101ded8
  1. 6
      spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java
  2. 2
      spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
  3. 10
      spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java
  4. 6
      spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java
  5. 2
      spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt

6
spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java

@ -54,14 +54,14 @@ public class FlashAttributeResultMatchers { @@ -54,14 +54,14 @@ public class FlashAttributeResultMatchers {
/**
* Assert a flash attribute's value.
*/
public <T> 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 <T> 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 { @@ -72,7 +72,7 @@ public class FlashAttributeResultMatchers {
/**
* Assert the number of flash attributes.
*/
public <T> ResultMatcher attributeCount(int count) {
public ResultMatcher attributeCount(int count) {
return result -> assertEquals("FlashMap size", count, result.getFlashMap().size());
}

2
spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java

@ -65,7 +65,7 @@ public class HeaderResultMatchers { @@ -65,7 +65,7 @@ public class HeaderResultMatchers {
* Iterable {@link Matcher}.
* @since 4.3
*/
public <T> ResultMatcher stringValues(String name, Matcher<Iterable<String>> matcher) {
public ResultMatcher stringValues(String name, Matcher<Iterable<String>> matcher) {
return result -> {
List<String> values = result.getResponse().getHeaders(name);
assertThat("Response header '" + name + "'", values, matcher);

10
spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java

@ -172,7 +172,7 @@ public class ModelResultMatchers { @@ -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 <T> ResultMatcher attributeHasFieldErrorCode(String name, String fieldName,
public ResultMatcher attributeHasFieldErrorCode(String name, String fieldName,
Matcher<? super String> matcher) {
return mvcResult -> {
@ -189,7 +189,7 @@ public class ModelResultMatchers { @@ -189,7 +189,7 @@ public class ModelResultMatchers {
/**
* Assert the total number of errors in the model.
*/
public <T> 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 { @@ -199,7 +199,7 @@ public class ModelResultMatchers {
/**
* Assert the model has errors.
*/
public <T> 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 { @@ -209,7 +209,7 @@ public class ModelResultMatchers {
/**
* Assert the model has no errors.
*/
public <T> ResultMatcher hasNoErrors() {
public ResultMatcher hasNoErrors() {
return result -> {
ModelAndView mav = getModelAndView(result);
for (Object value : mav.getModel().values()) {
@ -223,7 +223,7 @@ public class ModelResultMatchers { @@ -223,7 +223,7 @@ public class ModelResultMatchers {
/**
* Assert the number of model attributes.
*/
public <T> ResultMatcher size(int size) {
public ResultMatcher size(int size) {
return result -> {
ModelAndView mav = getModelAndView(result);
int actual = 0;

6
spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java

@ -98,7 +98,7 @@ public class RequestResultMatchers { @@ -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 <T> ResultMatcher asyncResult(Object expectedResult) {
public ResultMatcher asyncResult(Object expectedResult) {
return result -> {
HttpServletRequest request = result.getRequest();
assertAsyncStarted(request);
@ -120,7 +120,7 @@ public class RequestResultMatchers { @@ -120,7 +120,7 @@ public class RequestResultMatchers {
/**
* Assert a request attribute value.
*/
public <T> 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 { @@ -141,7 +141,7 @@ public class RequestResultMatchers {
/**
* Assert a session attribute value.
*/
public <T> 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");

2
spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt

@ -130,7 +130,7 @@ class MockMvcExtensionsTests { @@ -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<String>("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") }

Loading…
Cancel
Save