1- /*
2- * To change this license header, choose License Headers in Project Properties.
3- * To change this template file, choose Tools | Templates
4- * and open the template in the editor.
5- */
61package com .baeldung .reactive .errorhandling ;
72
83import java .io .IOException ;
1813
1914@ RunWith (SpringRunner .class )
2015@ SpringBootTest (webEnvironment = WebEnvironment .DEFINED_PORT )
21- public class ApplicationTest {
16+ public class ErrorHandlingIntegrationTest {
2217
2318 @ Autowired
2419 private WebTestClient webTestClient ;
2520
2621 @ Test
27- public void testLocalErrorHandlingUsingOnErrorReturn () throws IOException {
22+ public void givenErrorReturn_whenUsernamePresent_thenOk () throws IOException {
2823
29- System.out.println("Testing local error handling using onErrorReturn");
30-
31- // Pass a username
32- String i = webTestClient.get()
24+ String s = webTestClient .get ()
3325 .uri ("/api/endpoint1?name={username}" , "Tony" )
3426 .accept (MediaType .TEXT_PLAIN )
3527 .exchange ()
3628 .returnResult (String .class )
3729 .getResponseBody ()
3830 .blockFirst ();
3931
40- assertEquals("Hello, Tony", i);
32+ assertEquals ("Hello, Tony" , s );
33+
34+ }
4135
42- // Do not pass a username
43- i = webTestClient.get()
36+ @ Test
37+ public void givenErrorReturn_whenNoUsername_thenOk () throws IOException {
38+
39+ String s = webTestClient .get ()
4440 .uri ("/api/endpoint1" )
4541 .accept (MediaType .TEXT_PLAIN )
4642 .exchange ()
4743 .returnResult (String .class )
4844 .getResponseBody ()
4945 .blockFirst ();
5046
51- assertEquals("Hello, Stranger", i);
52-
47+ assertEquals ("Hello, Stranger" , s );
5348 }
5449
5550 @ Test
56- public void testLocalErrorHandlingUsingOnErrorResumeWithFallback() throws IOException {
57-
58- System.out.println("Testing local error handling using onErrorResume with fallback");
51+ public void givenResumeFallback_whenUsernamePresent_thenOk () throws IOException {
5952
60- // Pass a username
61- String i = webTestClient.get()
53+ String s = webTestClient .get ()
6254 .uri ("/api/endpoint2?name={username}" , "Tony" )
6355 .accept (MediaType .TEXT_PLAIN )
6456 .exchange ()
6557 .returnResult (String .class )
6658 .getResponseBody ()
6759 .blockFirst ();
6860
69- assertEquals("Hello, Tony", i);
61+ assertEquals ("Hello, Tony" , s );
62+ }
7063
71- // Do not pass a username
72- i = webTestClient.get()
64+ @ Test
65+ public void givenResumeFallback_whenNoUsername_thenOk () throws IOException {
66+ String s = webTestClient .get ()
7367 .uri ("/api/endpoint2" )
7468 .accept (MediaType .TEXT_PLAIN )
7569 .exchange ()
7670 .returnResult (String .class )
7771 .getResponseBody ()
7872 .blockFirst ();
7973
80- assertEquals("Hello, Stranger", i );
74+ assertEquals ("Hello, Stranger" , s );
8175
8276 }
8377
8478 @ Test
85- public void testLocalErrorHandlingUsingOnErrorResumeWithDynamicFallbackValue () throws IOException {
79+ public void givenResumeDynamicValue_whenUsernamePresent_thenOk () throws IOException {
8680
87- System.out.println("Testing local error handling using onErrorResume with dynamic fallback value");
88-
89- // Pass a username
90- String i = webTestClient.get()
81+ String s = webTestClient .get ()
9182 .uri ("/api/endpoint3?name={username}" , "Tony" )
9283 .accept (MediaType .TEXT_PLAIN )
9384 .exchange ()
9485 .returnResult (String .class )
9586 .getResponseBody ()
9687 .blockFirst ();
9788
98- assertEquals("Hello, Tony", i);
89+ assertEquals ("Hello, Tony" , s );
90+ }
9991
100- // Do not pass a username
101- i = webTestClient.get()
92+ @ Test
93+ public void givenResumeDynamicValue_whenNoUsername_thenOk () throws IOException {
94+ String s = webTestClient .get ()
10295 .uri ("/api/endpoint3" )
10396 .accept (MediaType .TEXT_PLAIN )
10497 .exchange ()
10598 .returnResult (String .class )
10699 .getResponseBody ()
107100 .blockFirst ();
108101
109- assertEquals("Hi, I looked around for your name but found: No value present", i);
110-
102+ assertEquals ("Hi, I looked around for your name but found: No value present" , s );
111103 }
112-
113- @Test
114- public void testLocalErrorHandlingUsingOnErrorResumeWithCatchAndRethrow() throws IOException {
115-
116- System.out.println("Testing local error handling using onErrorResume with catch and rethrow");
117104
118- // Pass a username
119- String i = webTestClient.get()
105+ @ Test
106+ public void givenResumeRethrow_whenUsernamePresent_thenOk () throws IOException {
107+ String s = webTestClient .get ()
120108 .uri ("/api/endpoint4?name={username}" , "Tony" )
121109 .accept (MediaType .TEXT_PLAIN )
122110 .exchange ()
123111 .returnResult (String .class )
124112 .getResponseBody ()
125113 .blockFirst ();
126114
127- assertEquals("Hello, Tony", i);
115+ assertEquals ("Hello, Tony" , s );
116+ }
117+
118+ @ Test
119+ public void givenResumeRethrow_whenNoUsername_thenOk () throws IOException {
128120
129- // Do not pass a username
130121 webTestClient .get ()
131122 .uri ("/api/endpoint4" )
132123 .accept (MediaType .TEXT_PLAIN )
@@ -140,26 +131,24 @@ public void testLocalErrorHandlingUsingOnErrorResumeWithCatchAndRethrow() throws
140131 .isNotEmpty ()
141132 .jsonPath ("$.message" )
142133 .isEqualTo ("please provide a name" );
143-
144134 }
145-
146- @Test
147- public void testGlobalErrorHandlingUsingErrorWebExceptionHandler() throws IOException {
148135
149- System.out.println("Testing local error handling using ErrorWebExceptionHandler");
136+ @ Test
137+ public void givenGlobalErrorHandling_whenUsernamePresent_thenOk () throws IOException {
150138
151- // Pass a username
152- String i = webTestClient.get()
139+ String s = webTestClient .get ()
153140 .uri ("/api/endpoint5?name={username}" , "Tony" )
154141 .accept (MediaType .TEXT_PLAIN )
155142 .exchange ()
156143 .returnResult (String .class )
157144 .getResponseBody ()
158145 .blockFirst ();
159146
160- assertEquals("Hello, Tony", i);
161-
162- // Do not pass a username
147+ assertEquals ("Hello, Tony" , s );
148+ }
149+
150+ @ Test
151+ public void givenGlobalErrorHandling_whenNoUsername_thenOk () throws IOException {
163152 webTestClient .get ()
164153 .uri ("/api/endpoint5" )
165154 .accept (MediaType .TEXT_PLAIN )
@@ -175,5 +164,5 @@ public void testGlobalErrorHandlingUsingErrorWebExceptionHandler() throws IOExce
175164 .isEqualTo ("please provide a name" );
176165
177166 }
178-
167+
179168}
0 commit comments