Browse Source

Polish tests

pull/35599/head
Sam Brannen 2 months ago
parent
commit
19b4021d26
  1. 19
      spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java
  2. 2
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java
  3. 7
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java

19
spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java

@ -208,14 +208,12 @@ class ExceptionHandlerMethodResolverTests {
@Controller @Controller
static class MediaTypeController { static class MediaTypeController {
@ExceptionHandler(exception = {IllegalArgumentException.class}, produces = "application/json") @ExceptionHandler(exception = IllegalArgumentException.class, produces = "application/json")
public void handleJson() { public void handleJson() {
} }
@ExceptionHandler(exception = {IllegalArgumentException.class}, produces = {"text/html", "*/*"}) @ExceptionHandler(exception = IllegalArgumentException.class, produces = {"text/html", "*/*"})
public void handleHtml() { public void handleHtml() {
} }
} }
@ -223,14 +221,12 @@ class ExceptionHandlerMethodResolverTests {
@Controller @Controller
static class AmbiguousMediaTypeController { static class AmbiguousMediaTypeController {
@ExceptionHandler(exception = {IllegalArgumentException.class}, produces = "application/json") @ExceptionHandler(exception = IllegalArgumentException.class, produces = "application/json")
public void handleJson() { public void handleJson() {
} }
@ExceptionHandler(exception = {IllegalArgumentException.class}, produces = "application/json") @ExceptionHandler(exception = IllegalArgumentException.class, produces = "application/json")
public void handleJsonToo() { public void handleJsonToo() {
} }
} }
@ -238,14 +234,12 @@ class ExceptionHandlerMethodResolverTests {
@Controller @Controller
static class MixedController { static class MixedController {
@ExceptionHandler(exception = {IllegalArgumentException.class}, produces = "application/json") @ExceptionHandler(exception = IllegalArgumentException.class, produces = "application/json")
public void handleJson() { public void handleJson() {
} }
@ExceptionHandler(IllegalArgumentException.class) @ExceptionHandler(IllegalArgumentException.class)
public void handleOther() { public void handleOther() {
} }
} }
@ -253,9 +247,8 @@ class ExceptionHandlerMethodResolverTests {
@Controller @Controller
static class InvalidMediaTypeController { static class InvalidMediaTypeController {
@ExceptionHandler(exception = {IllegalArgumentException.class}, produces = "invalid-mediatype") @ExceptionHandler(exception = IllegalArgumentException.class, produces = "invalid-mediatype")
public void handle() { public void handle() {
} }
} }

2
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java

@ -544,7 +544,7 @@ class ExceptionHandlerExceptionResolverTests {
public void handle() {} public void handle() {}
@ExceptionHandler(value = IOException.class) @ExceptionHandler(IOException.class)
public void handleException() { public void handleException() {
} }
} }

7
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java

@ -97,22 +97,21 @@ class ResponseEntityExceptionHandlerTests {
private WebRequest request = new ServletWebRequest(this.servletRequest, this.servletResponse); private WebRequest request = new ServletWebRequest(this.servletRequest, this.servletResponse);
@SuppressWarnings("unchecked")
@Test @Test
void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception { void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception {
ExceptionHandler annotation = ResponseEntityExceptionHandler.class ExceptionHandler annotation = ResponseEntityExceptionHandler.class
.getMethod("handleException", Exception.class, WebRequest.class) .getMethod("handleException", Exception.class, WebRequest.class)
.getAnnotation(ExceptionHandler.class); .getAnnotation(ExceptionHandler.class);
Class<?>[] exceptionTypes = annotation.value();
Arrays.stream(DefaultHandlerExceptionResolver.class.getDeclaredMethods()) Arrays.stream(DefaultHandlerExceptionResolver.class.getDeclaredMethods())
.filter(method -> method.getName().startsWith("handle") && (method.getParameterCount() == 4)) .filter(method -> method.getName().startsWith("handle") && (method.getParameterCount() == 4))
.filter(method -> !method.getName().equals("handleErrorResponse")) .filter(method -> !method.getName().equals("handleErrorResponse"))
.filter(method -> !method.getName().equals("handleDisconnectedClientException")) .filter(method -> !method.getName().equals("handleDisconnectedClientException"))
.map(method -> method.getParameterTypes()[0]) .map(method -> method.getParameterTypes()[0])
.forEach(exceptionType -> assertThat(annotation.value()) .forEach(exceptionType -> assertThat(exceptionTypes)
.as("@ExceptionHandler is missing declaration for " + exceptionType.getName()) .as("@ExceptionHandler is missing declaration for " + exceptionType.getName())
.contains((Class<Exception>) exceptionType)); .contains(exceptionType));
} }
@Test @Test

Loading…
Cancel
Save