|
| 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 | + */ |
| 6 | +package com.baeldung.reactive.errorhandling; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 15 | +import org.springframework.http.MediaType; |
| 16 | +import org.springframework.test.context.junit4.SpringRunner; |
| 17 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 18 | + |
| 19 | +@RunWith(SpringRunner.class) |
| 20 | +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) |
| 21 | +public class ErrorHandlingTest { |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private WebTestClient webTestClient; |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testLocalErrorHandlingUsingOnErrorReturn() throws IOException { |
| 28 | + |
| 29 | + System.out.println("Testing local error handling using onErrorReturn"); |
| 30 | + |
| 31 | + // Pass a username |
| 32 | + String i = webTestClient.get() |
| 33 | + .uri("/api/endpoint1?name={username}", "Tony") |
| 34 | + .accept(MediaType.TEXT_PLAIN) |
| 35 | + .exchange() |
| 36 | + .returnResult(String.class) |
| 37 | + .getResponseBody() |
| 38 | + .blockFirst(); |
| 39 | + |
| 40 | + assertEquals("Hello, Tony", i); |
| 41 | + |
| 42 | + // Do not pass a username |
| 43 | + i = webTestClient.get() |
| 44 | + .uri("/api/endpoint1") |
| 45 | + .accept(MediaType.TEXT_PLAIN) |
| 46 | + .exchange() |
| 47 | + .returnResult(String.class) |
| 48 | + .getResponseBody() |
| 49 | + .blockFirst(); |
| 50 | + |
| 51 | + assertEquals("Hello, Stranger", i); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testLocalErrorHandlingUsingOnErrorResumeWithFallback() throws IOException { |
| 57 | + |
| 58 | + System.out.println("Testing local error handling using onErrorResume with fallback"); |
| 59 | + |
| 60 | + // Pass a username |
| 61 | + String i = webTestClient.get() |
| 62 | + .uri("/api/endpoint2?name={username}", "Tony") |
| 63 | + .accept(MediaType.TEXT_PLAIN) |
| 64 | + .exchange() |
| 65 | + .returnResult(String.class) |
| 66 | + .getResponseBody() |
| 67 | + .blockFirst(); |
| 68 | + |
| 69 | + assertEquals("Hello, Tony", i); |
| 70 | + |
| 71 | + // Do not pass a username |
| 72 | + i = webTestClient.get() |
| 73 | + .uri("/api/endpoint2") |
| 74 | + .accept(MediaType.TEXT_PLAIN) |
| 75 | + .exchange() |
| 76 | + .returnResult(String.class) |
| 77 | + .getResponseBody() |
| 78 | + .blockFirst(); |
| 79 | + |
| 80 | + assertEquals("Hello, Stranger", i); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testLocalErrorHandlingUsingOnErrorResumeWithDynamicFallbackValue() throws IOException { |
| 86 | + |
| 87 | + System.out.println("Testing local error handling using onErrorResume with dynamic fallback value"); |
| 88 | + |
| 89 | + // Pass a username |
| 90 | + String i = webTestClient.get() |
| 91 | + .uri("/api/endpoint3?name={username}", "Tony") |
| 92 | + .accept(MediaType.TEXT_PLAIN) |
| 93 | + .exchange() |
| 94 | + .returnResult(String.class) |
| 95 | + .getResponseBody() |
| 96 | + .blockFirst(); |
| 97 | + |
| 98 | + assertEquals("Hello, Tony", i); |
| 99 | + |
| 100 | + // Do not pass a username |
| 101 | + i = webTestClient.get() |
| 102 | + .uri("/api/endpoint3") |
| 103 | + .accept(MediaType.TEXT_PLAIN) |
| 104 | + .exchange() |
| 105 | + .returnResult(String.class) |
| 106 | + .getResponseBody() |
| 107 | + .blockFirst(); |
| 108 | + |
| 109 | + assertEquals("Hi, I looked around for your name but found: No value present", i); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testLocalErrorHandlingUsingOnErrorResumeWithCatchAndRethrow() throws IOException { |
| 115 | + |
| 116 | + System.out.println("Testing local error handling using onErrorResume with catch and rethrow"); |
| 117 | + |
| 118 | + // Pass a username |
| 119 | + String i = webTestClient.get() |
| 120 | + .uri("/api/endpoint4?name={username}", "Tony") |
| 121 | + .accept(MediaType.TEXT_PLAIN) |
| 122 | + .exchange() |
| 123 | + .returnResult(String.class) |
| 124 | + .getResponseBody() |
| 125 | + .blockFirst(); |
| 126 | + |
| 127 | + assertEquals("Hello, Tony", i); |
| 128 | + |
| 129 | + // Do not pass a username |
| 130 | + webTestClient.get() |
| 131 | + .uri("/api/endpoint4") |
| 132 | + .accept(MediaType.TEXT_PLAIN) |
| 133 | + .exchange() |
| 134 | + .expectStatus() |
| 135 | + .isBadRequest() |
| 136 | + .expectHeader() |
| 137 | + .contentType(MediaType.APPLICATION_JSON_UTF8) |
| 138 | + .expectBody() |
| 139 | + .jsonPath("$.message") |
| 140 | + .isNotEmpty() |
| 141 | + .jsonPath("$.message") |
| 142 | + .isEqualTo("please provide a name"); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testGlobalErrorHandlingUsingErrorWebExceptionHandler() throws IOException { |
| 148 | + |
| 149 | + System.out.println("Testing local error handling using ErrorWebExceptionHandler"); |
| 150 | + |
| 151 | + // Pass a username |
| 152 | + String i = webTestClient.get() |
| 153 | + .uri("/api/endpoint5?name={username}", "Tony") |
| 154 | + .accept(MediaType.TEXT_PLAIN) |
| 155 | + .exchange() |
| 156 | + .returnResult(String.class) |
| 157 | + .getResponseBody() |
| 158 | + .blockFirst(); |
| 159 | + |
| 160 | + assertEquals("Hello, Tony", i); |
| 161 | + |
| 162 | + // Do not pass a username |
| 163 | + webTestClient.get() |
| 164 | + .uri("/api/endpoint5") |
| 165 | + .accept(MediaType.TEXT_PLAIN) |
| 166 | + .exchange() |
| 167 | + .expectStatus() |
| 168 | + .isBadRequest() |
| 169 | + .expectHeader() |
| 170 | + .contentType(MediaType.APPLICATION_JSON_UTF8) |
| 171 | + .expectBody() |
| 172 | + .jsonPath("$.message") |
| 173 | + .isNotEmpty() |
| 174 | + .jsonPath("$.message") |
| 175 | + .isEqualTo("please provide a name"); |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments