refactor test
This commit is contained in:
		
							parent
							
								
									db00197aac
								
							
						
					
					
						commit
						a1dab9a874
					
				| @ -1,8 +1,3 @@ | |||||||
| /* |  | ||||||
|  * To change this license header, choose License Headers in Project Properties. |  | ||||||
|  * To change this template file, choose Tools | Templates |  | ||||||
|  * and open the template in the editor. |  | ||||||
|  */ |  | ||||||
| package com.baeldung.reactive.errorhandling; | package com.baeldung.reactive.errorhandling; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| @ -18,18 +13,15 @@ import org.springframework.test.web.reactive.server.WebTestClient; | |||||||
| 
 | 
 | ||||||
| @RunWith(SpringRunner.class) | @RunWith(SpringRunner.class) | ||||||
| @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) | @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) | ||||||
| public class ApplicationTest { | public class ErrorHandlingIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
|     private WebTestClient webTestClient; |     private WebTestClient webTestClient; | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void testLocalErrorHandlingUsingOnErrorReturn() throws IOException { |     public void givenErrorReturn_whenUsernamePresent_thenOk() throws IOException { | ||||||
| 
 | 
 | ||||||
|         System.out.println("Testing local error handling using onErrorReturn"); |         String s = webTestClient.get() | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint1?name={username}", "Tony") |             .uri("/api/endpoint1?name={username}", "Tony") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -37,10 +29,14 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hello, Tony", i); |         assertEquals("Hello, Tony", s); | ||||||
| 
 | 
 | ||||||
|         // Do not pass a username |     } | ||||||
|         i = webTestClient.get() | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenErrorReturn_whenNoUsername_thenOk() throws IOException { | ||||||
|  | 
 | ||||||
|  |         String s = webTestClient.get() | ||||||
|             .uri("/api/endpoint1") |             .uri("/api/endpoint1") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -48,17 +44,13 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hello, Stranger", i); |         assertEquals("Hello, Stranger", s); | ||||||
| 
 |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void testLocalErrorHandlingUsingOnErrorResumeWithFallback() throws IOException { |     public void givenResumeFallback_whenUsernamePresent_thenOk() throws IOException { | ||||||
| 
 | 
 | ||||||
|         System.out.println("Testing local error handling using onErrorResume with fallback"); |         String s = webTestClient.get() | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint2?name={username}", "Tony") |             .uri("/api/endpoint2?name={username}", "Tony") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -66,10 +58,12 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hello, Tony", i); |         assertEquals("Hello, Tony", s); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         // Do not pass a username |     @Test | ||||||
|         i = webTestClient.get() |     public void givenResumeFallback_whenNoUsername_thenOk() throws IOException { | ||||||
|  |         String s = webTestClient.get() | ||||||
|             .uri("/api/endpoint2") |             .uri("/api/endpoint2") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -77,17 +71,14 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hello, Stranger", i); |         assertEquals("Hello, Stranger", s); | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void testLocalErrorHandlingUsingOnErrorResumeWithDynamicFallbackValue() throws IOException { |     public void givenResumeDynamicValue_whenUsernamePresent_thenOk() throws IOException { | ||||||
| 
 | 
 | ||||||
|         System.out.println("Testing local error handling using onErrorResume with dynamic fallback value"); |         String s = webTestClient.get() | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint3?name={username}", "Tony") |             .uri("/api/endpoint3?name={username}", "Tony") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -95,10 +86,12 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hello, Tony", i); |         assertEquals("Hello, Tony", s); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         // Do not pass a username |     @Test | ||||||
|         i = webTestClient.get() |     public void givenResumeDynamicValue_whenNoUsername_thenOk() throws IOException { | ||||||
|  |         String s = webTestClient.get() | ||||||
|             .uri("/api/endpoint3") |             .uri("/api/endpoint3") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -106,17 +99,12 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hi, I looked around for your name but found: No value present", i); |         assertEquals("Hi, I looked around for your name but found: No value present", s); | ||||||
| 
 |  | ||||||
|     } |     } | ||||||
|      | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void testLocalErrorHandlingUsingOnErrorResumeWithCatchAndRethrow() throws IOException { |     public void givenResumeRethrow_whenUsernamePresent_thenOk() throws IOException { | ||||||
| 
 |         String s = webTestClient.get() | ||||||
|         System.out.println("Testing local error handling using onErrorResume with catch and rethrow"); |  | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint4?name={username}", "Tony") |             .uri("/api/endpoint4?name={username}", "Tony") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -124,9 +112,12 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hello, Tony", i); |         assertEquals("Hello, Tony", s); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenResumeRethrow_whenNoUsername_thenOk() throws IOException { | ||||||
| 
 | 
 | ||||||
|         // Do not pass a username |  | ||||||
|         webTestClient.get() |         webTestClient.get() | ||||||
|             .uri("/api/endpoint4") |             .uri("/api/endpoint4") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
| @ -140,16 +131,12 @@ public class ApplicationTest { | |||||||
|             .isNotEmpty() |             .isNotEmpty() | ||||||
|             .jsonPath("$.message") |             .jsonPath("$.message") | ||||||
|             .isEqualTo("please provide a name"); |             .isEqualTo("please provide a name"); | ||||||
| 
 |  | ||||||
|     } |     } | ||||||
|          | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void testGlobalErrorHandlingUsingErrorWebExceptionHandler() throws IOException { |     public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() throws IOException { | ||||||
| 
 | 
 | ||||||
|         System.out.println("Testing local error handling using ErrorWebExceptionHandler"); |         String s = webTestClient.get() | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint5?name={username}", "Tony") |             .uri("/api/endpoint5?name={username}", "Tony") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
|             .exchange() |             .exchange() | ||||||
| @ -157,9 +144,11 @@ public class ApplicationTest { | |||||||
|             .getResponseBody() |             .getResponseBody() | ||||||
|             .blockFirst(); |             .blockFirst(); | ||||||
| 
 | 
 | ||||||
|         assertEquals("Hello, Tony", i); |         assertEquals("Hello, Tony", s); | ||||||
| 
 |     } | ||||||
|         // Do not pass a username |      | ||||||
|  |     @Test | ||||||
|  |     public void givenGlobalErrorHandling_whenNoUsername_thenOk() throws IOException { | ||||||
|         webTestClient.get() |         webTestClient.get() | ||||||
|             .uri("/api/endpoint5") |             .uri("/api/endpoint5") | ||||||
|             .accept(MediaType.TEXT_PLAIN) |             .accept(MediaType.TEXT_PLAIN) | ||||||
| @ -175,5 +164,5 @@ public class ApplicationTest { | |||||||
|             .isEqualTo("please provide a name"); |             .isEqualTo("please provide a name"); | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
|              | 
 | ||||||
| } | } | ||||||
| @ -1,179 +0,0 @@ | |||||||
| /* |  | ||||||
|  * To change this license header, choose License Headers in Project Properties. |  | ||||||
|  * To change this template file, choose Tools | Templates |  | ||||||
|  * and open the template in the editor. |  | ||||||
|  */ |  | ||||||
| package com.baeldung.reactive.errorhandling; |  | ||||||
| 
 |  | ||||||
| import java.io.IOException; |  | ||||||
| import static org.junit.Assert.assertEquals; |  | ||||||
| import org.junit.Test; |  | ||||||
| import org.junit.runner.RunWith; |  | ||||||
| import org.springframework.beans.factory.annotation.Autowired; |  | ||||||
| import org.springframework.boot.test.context.SpringBootTest; |  | ||||||
| import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |  | ||||||
| import org.springframework.http.MediaType; |  | ||||||
| import org.springframework.test.context.junit4.SpringRunner; |  | ||||||
| import org.springframework.test.web.reactive.server.WebTestClient; |  | ||||||
| 
 |  | ||||||
| @RunWith(SpringRunner.class) |  | ||||||
| @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) |  | ||||||
| public class ErrorHandlingTest { |  | ||||||
| 
 |  | ||||||
|     @Autowired |  | ||||||
|     private WebTestClient webTestClient; |  | ||||||
| 
 |  | ||||||
|     @Test |  | ||||||
|     public void testLocalErrorHandlingUsingOnErrorReturn() throws IOException { |  | ||||||
| 
 |  | ||||||
|         System.out.println("Testing local error handling using onErrorReturn"); |  | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint1?name={username}", "Tony") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hello, Tony", i); |  | ||||||
| 
 |  | ||||||
|         // Do not pass a username |  | ||||||
|         i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint1") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hello, Stranger", i); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Test |  | ||||||
|     public void testLocalErrorHandlingUsingOnErrorResumeWithFallback() throws IOException { |  | ||||||
| 
 |  | ||||||
|         System.out.println("Testing local error handling using onErrorResume with fallback"); |  | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint2?name={username}", "Tony") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hello, Tony", i); |  | ||||||
| 
 |  | ||||||
|         // Do not pass a username |  | ||||||
|         i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint2") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hello, Stranger", i); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Test |  | ||||||
|     public void testLocalErrorHandlingUsingOnErrorResumeWithDynamicFallbackValue() throws IOException { |  | ||||||
| 
 |  | ||||||
|         System.out.println("Testing local error handling using onErrorResume with dynamic fallback value"); |  | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint3?name={username}", "Tony") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hello, Tony", i); |  | ||||||
| 
 |  | ||||||
|         // Do not pass a username |  | ||||||
|         i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint3") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hi, I looked around for your name but found: No value present", i); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     @Test |  | ||||||
|     public void testLocalErrorHandlingUsingOnErrorResumeWithCatchAndRethrow() throws IOException { |  | ||||||
| 
 |  | ||||||
|         System.out.println("Testing local error handling using onErrorResume with catch and rethrow"); |  | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint4?name={username}", "Tony") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hello, Tony", i); |  | ||||||
| 
 |  | ||||||
|         // Do not pass a username |  | ||||||
|         webTestClient.get() |  | ||||||
|             .uri("/api/endpoint4") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .expectStatus() |  | ||||||
|             .isBadRequest() |  | ||||||
|             .expectHeader() |  | ||||||
|             .contentType(MediaType.APPLICATION_JSON_UTF8) |  | ||||||
|             .expectBody() |  | ||||||
|             .jsonPath("$.message") |  | ||||||
|             .isNotEmpty() |  | ||||||
|             .jsonPath("$.message") |  | ||||||
|             .isEqualTo("please provide a name"); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
|          |  | ||||||
|     @Test |  | ||||||
|     public void testGlobalErrorHandlingUsingErrorWebExceptionHandler() throws IOException { |  | ||||||
| 
 |  | ||||||
|         System.out.println("Testing local error handling using ErrorWebExceptionHandler"); |  | ||||||
| 
 |  | ||||||
|         // Pass a username |  | ||||||
|         String i = webTestClient.get() |  | ||||||
|             .uri("/api/endpoint5?name={username}", "Tony") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .returnResult(String.class) |  | ||||||
|             .getResponseBody() |  | ||||||
|             .blockFirst(); |  | ||||||
| 
 |  | ||||||
|         assertEquals("Hello, Tony", i); |  | ||||||
| 
 |  | ||||||
|         // Do not pass a username |  | ||||||
|         webTestClient.get() |  | ||||||
|             .uri("/api/endpoint5") |  | ||||||
|             .accept(MediaType.TEXT_PLAIN) |  | ||||||
|             .exchange() |  | ||||||
|             .expectStatus() |  | ||||||
|             .isBadRequest() |  | ||||||
|             .expectHeader() |  | ||||||
|             .contentType(MediaType.APPLICATION_JSON_UTF8) |  | ||||||
|             .expectBody() |  | ||||||
|             .jsonPath("$.message") |  | ||||||
|             .isNotEmpty() |  | ||||||
|             .jsonPath("$.message") |  | ||||||
|             .isEqualTo("please provide a name"); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
|              |  | ||||||
| } |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user