Browse Source

Unit tests for @MessageExceptionHandler cause and error resolution

Issue: SPR-16912
pull/1854/head
Juergen Hoeller 7 years ago
parent
commit
646d7f9e57
  1. 8
      spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolverTests.java
  2. 52
      spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

8
spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolverTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -86,6 +86,12 @@ public class AnnotationExceptionHandlerMethodResolverTests { @@ -86,6 +86,12 @@ public class AnnotationExceptionHandlerMethodResolverTests {
assertEquals("handleIOException", this.resolver.resolveMethod(exception).getName());
}
@Test
public void resolveMethodAgainstCause() {
IllegalStateException exception = new IllegalStateException(new IOException());
assertEquals("handleIOException", this.resolver.resolveMethod(exception).getName());
}
@Test(expected = IllegalStateException.class)
public void ambiguousExceptionMapping() {
new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class);

52
spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -75,7 +75,6 @@ import static org.mockito.BDDMockito.*; @@ -75,7 +75,6 @@ import static org.mockito.BDDMockito.*;
* @author Brian Clozel
* @author Sebastien Deleuze
*/
@SuppressWarnings("unused")
public class SimpAnnotationMethodMessageHandlerTests {
private static final String TEST_INVALID_VALUE = "invalidValue";
@ -201,6 +200,30 @@ public class SimpAnnotationMethodMessageHandlerTests { @@ -201,6 +200,30 @@ public class SimpAnnotationMethodMessageHandlerTests {
assertEquals("illegalState", handlerMethod.getMethod().getName());
}
@Test
public void exceptionAsCause() {
Message<?> message = createMessage("/pre/illegalStateCause");
this.messageHandler.registerHandler(this.testController);
this.messageHandler.handleMessage(message);
assertEquals("handleExceptionWithHandlerMethodArg", this.testController.method);
HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod");
assertNotNull(handlerMethod);
assertEquals("illegalStateCause", handlerMethod.getMethod().getName());
}
@Test
public void errorAsMessageHandlingException() {
Message<?> message = createMessage("/pre/error");
this.messageHandler.registerHandler(this.testController);
this.messageHandler.handleMessage(message);
assertEquals("handleErrorWithHandlerMethodArg", this.testController.method);
HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod");
assertNotNull(handlerMethod);
assertEquals("errorAsThrowable", handlerMethod.getMethod().getName());
}
@Test
public void simpScope() {
Map<String, Object> sessionAttributes = new ConcurrentHashMap<>();
@ -412,7 +435,17 @@ public class SimpAnnotationMethodMessageHandlerTests { @@ -412,7 +435,17 @@ public class SimpAnnotationMethodMessageHandlerTests {
@MessageMapping("/illegalState")
public void illegalState() {
throw new IllegalStateException();
throw new IllegalStateException("my cause");
}
@MessageMapping("/illegalStateCause")
public void illegalStateCause() {
throw new RuntimeException(new IllegalStateException("my cause"));
}
@MessageMapping("/error")
public void errorAsThrowable() {
throw new Error("my cause");
}
@MessageExceptionHandler(MethodArgumentNotValidException.class)
@ -420,10 +453,18 @@ public class SimpAnnotationMethodMessageHandlerTests { @@ -420,10 +453,18 @@ public class SimpAnnotationMethodMessageHandlerTests {
this.method = "handleValidationException";
}
@MessageExceptionHandler(IllegalStateException.class)
public void handleExceptionWithHandlerMethodArg(HandlerMethod handlerMethod) {
@MessageExceptionHandler
public void handleExceptionWithHandlerMethodArg(IllegalStateException ex, HandlerMethod handlerMethod) {
this.method = "handleExceptionWithHandlerMethodArg";
this.arguments.put("handlerMethod", handlerMethod);
assertEquals("my cause", ex.getMessage());
}
@MessageExceptionHandler
public void handleErrorWithHandlerMethodArg(Error ex, HandlerMethod handlerMethod) {
this.method = "handleErrorWithHandlerMethodArg";
this.arguments.put("handlerMethod", handlerMethod);
assertEquals("my cause", ex.getMessage());
}
@MessageMapping("/scope")
@ -446,7 +487,6 @@ public class SimpAnnotationMethodMessageHandlerTests { @@ -446,7 +487,6 @@ public class SimpAnnotationMethodMessageHandlerTests {
private String method;
@MessageMapping("foo")
public void handleFoo() {
this.method = "handleFoo";

Loading…
Cancel
Save