|
|
@ -16,6 +16,8 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.web.reactive.result.method.annotation; |
|
|
|
package org.springframework.web.reactive.result.method.annotation; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.reactivestreams.Publisher; |
|
|
|
import org.reactivestreams.Publisher; |
|
|
|
import reactor.core.publisher.Mono; |
|
|
|
import reactor.core.publisher.Mono; |
|
|
@ -37,6 +39,7 @@ import static org.junit.Assert.*; |
|
|
|
* {@code @RequestMapping} integration tests with exception handling scenarios. |
|
|
|
* {@code @RequestMapping} integration tests with exception handling scenarios. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMappingIntegrationTests { |
|
|
|
public class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMappingIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
@ -55,6 +58,18 @@ public class RequestMappingExceptionHandlingIntegrationTests extends AbstractReq |
|
|
|
assertEquals(expected, performGet("/thrown-exception", new HttpHeaders(), String.class).getBody()); |
|
|
|
assertEquals(expected, performGet("/thrown-exception", new HttpHeaders(), String.class).getBody()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void controllerThrowingExceptionWithCause() throws Exception { |
|
|
|
|
|
|
|
String expected = "Recovered from error: State"; |
|
|
|
|
|
|
|
assertEquals(expected, performGet("/thrown-exception-with-cause", new HttpHeaders(), String.class).getBody()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void controllerThrowingExceptionWithCauseToHandle() throws Exception { |
|
|
|
|
|
|
|
String expected = "Recovered from error: IO"; |
|
|
|
|
|
|
|
assertEquals(expected, performGet("/thrown-exception-with-cause-to-handle", new HttpHeaders(), String.class).getBody()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void controllerReturnsMonoError() throws Exception { |
|
|
|
public void controllerReturnsMonoError() throws Exception { |
|
|
|
String expected = "Recovered from error: Argument"; |
|
|
|
String expected = "Recovered from error: Argument"; |
|
|
@ -79,11 +94,26 @@ public class RequestMappingExceptionHandlingIntegrationTests extends AbstractReq |
|
|
|
throw new IllegalStateException("State"); |
|
|
|
throw new IllegalStateException("State"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/thrown-exception-with-cause") |
|
|
|
|
|
|
|
public Publisher<String> handleAndThrowExceptionWithCause() { |
|
|
|
|
|
|
|
throw new IllegalStateException("State", new IOException("IO")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/thrown-exception-with-cause-to-handle") |
|
|
|
|
|
|
|
public Publisher<String> handleAndThrowExceptionWithCauseToHandle() { |
|
|
|
|
|
|
|
throw new RuntimeException("State", new IOException("IO")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/mono-error") |
|
|
|
@GetMapping("/mono-error") |
|
|
|
public Publisher<String> handleWithError() { |
|
|
|
public Publisher<String> handleWithError() { |
|
|
|
return Mono.error(new IllegalArgumentException("Argument")); |
|
|
|
return Mono.error(new IllegalArgumentException("Argument")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ExceptionHandler |
|
|
|
|
|
|
|
public Publisher<String> handleArgumentException(IOException ex) { |
|
|
|
|
|
|
|
return Mono.just("Recovered from error: " + ex.getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ExceptionHandler |
|
|
|
@ExceptionHandler |
|
|
|
public Publisher<String> handleArgumentException(IllegalArgumentException ex) { |
|
|
|
public Publisher<String> handleArgumentException(IllegalArgumentException ex) { |
|
|
|
return Mono.just("Recovered from error: " + ex.getMessage()); |
|
|
|
return Mono.just("Recovered from error: " + ex.getMessage()); |
|
|
|