Unit tests for @MessageExceptionHandler cause and error resolution
Issue: SPR-16912
This commit is contained in:
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -86,6 +86,12 @@ public class AnnotationExceptionHandlerMethodResolverTests {
|
|||||||
assertEquals("handleIOException", this.resolver.resolveMethod(exception).getName());
|
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)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void ambiguousExceptionMapping() {
|
public void ambiguousExceptionMapping() {
|
||||||
new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class);
|
new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class);
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -75,7 +75,6 @@ import static org.mockito.BDDMockito.*;
|
|||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public class SimpAnnotationMethodMessageHandlerTests {
|
public class SimpAnnotationMethodMessageHandlerTests {
|
||||||
|
|
||||||
private static final String TEST_INVALID_VALUE = "invalidValue";
|
private static final String TEST_INVALID_VALUE = "invalidValue";
|
||||||
@@ -201,6 +200,30 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
|||||||
assertEquals("illegalState", handlerMethod.getMethod().getName());
|
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
|
@Test
|
||||||
public void simpScope() {
|
public void simpScope() {
|
||||||
Map<String, Object> sessionAttributes = new ConcurrentHashMap<>();
|
Map<String, Object> sessionAttributes = new ConcurrentHashMap<>();
|
||||||
@@ -412,7 +435,17 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
|||||||
|
|
||||||
@MessageMapping("/illegalState")
|
@MessageMapping("/illegalState")
|
||||||
public void 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)
|
@MessageExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
@@ -420,10 +453,18 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
|||||||
this.method = "handleValidationException";
|
this.method = "handleValidationException";
|
||||||
}
|
}
|
||||||
|
|
||||||
@MessageExceptionHandler(IllegalStateException.class)
|
@MessageExceptionHandler
|
||||||
public void handleExceptionWithHandlerMethodArg(HandlerMethod handlerMethod) {
|
public void handleExceptionWithHandlerMethodArg(IllegalStateException ex, HandlerMethod handlerMethod) {
|
||||||
this.method = "handleExceptionWithHandlerMethodArg";
|
this.method = "handleExceptionWithHandlerMethodArg";
|
||||||
this.arguments.put("handlerMethod", handlerMethod);
|
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")
|
@MessageMapping("/scope")
|
||||||
@@ -446,7 +487,6 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
|||||||
|
|
||||||
private String method;
|
private String method;
|
||||||
|
|
||||||
|
|
||||||
@MessageMapping("foo")
|
@MessageMapping("foo")
|
||||||
public void handleFoo() {
|
public void handleFoo() {
|
||||||
this.method = "handleFoo";
|
this.method = "handleFoo";
|
||||||
|
|||||||
Reference in New Issue
Block a user