1616
1717package org .springframework .web .reactive .result .method .annotation ;
1818
19+ import java .io .IOException ;
20+
1921import org .junit .Test ;
2022import org .reactivestreams .Publisher ;
2123import reactor .core .publisher .Mono ;
3739 * {@code @RequestMapping} integration tests with exception handling scenarios.
3840 *
3941 * @author Rossen Stoyanchev
42+ * @author Juergen Hoeller
4043 */
4144public class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMappingIntegrationTests {
4245
@@ -55,6 +58,18 @@ public void controllerThrowingException() throws Exception {
5558 assertEquals (expected , performGet ("/thrown-exception" , new HttpHeaders (), String .class ).getBody ());
5659 }
5760
61+ @ Test
62+ public void controllerThrowingExceptionWithCause () throws Exception {
63+ String expected = "Recovered from error: State" ;
64+ assertEquals (expected , performGet ("/thrown-exception-with-cause" , new HttpHeaders (), String .class ).getBody ());
65+ }
66+
67+ @ Test
68+ public void controllerThrowingExceptionWithCauseToHandle () throws Exception {
69+ String expected = "Recovered from error: IO" ;
70+ assertEquals (expected , performGet ("/thrown-exception-with-cause-to-handle" , new HttpHeaders (), String .class ).getBody ());
71+ }
72+
5873 @ Test
5974 public void controllerReturnsMonoError () throws Exception {
6075 String expected = "Recovered from error: Argument" ;
@@ -79,11 +94,26 @@ public Publisher<String> handleAndThrowException() {
7994 throw new IllegalStateException ("State" );
8095 }
8196
97+ @ GetMapping ("/thrown-exception-with-cause" )
98+ public Publisher <String > handleAndThrowExceptionWithCause () {
99+ throw new IllegalStateException ("State" , new IOException ("IO" ));
100+ }
101+
102+ @ GetMapping ("/thrown-exception-with-cause-to-handle" )
103+ public Publisher <String > handleAndThrowExceptionWithCauseToHandle () {
104+ throw new RuntimeException ("State" , new IOException ("IO" ));
105+ }
106+
82107 @ GetMapping ("/mono-error" )
83108 public Publisher <String > handleWithError () {
84109 return Mono .error (new IllegalArgumentException ("Argument" ));
85110 }
86111
112+ @ ExceptionHandler
113+ public Publisher <String > handleArgumentException (IOException ex ) {
114+ return Mono .just ("Recovered from error: " + ex .getMessage ());
115+ }
116+
87117 @ ExceptionHandler
88118 public Publisher <String > handleArgumentException (IllegalArgumentException ex ) {
89119 return Mono .just ("Recovered from error: " + ex .getMessage ());
0 commit comments