From 1e69e96e7b54474a88ee593e8c12d14e63d469a2 Mon Sep 17 00:00:00 2001 From: Tonnix Date: Mon, 9 Jul 2018 13:25:02 +0300 Subject: [PATCH] Added test file for BAEL-1867 --- .../errorhandling/ErrorHandlingTest.java | 179 ++++++++++++++++++ .../errorhandling/ErrorHandlingTest.java~ | 179 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java~ diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java new file mode 100644 index 0000000000..b1be911419 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java @@ -0,0 +1,179 @@ +/* + * 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"); + + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java~ b/spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java~ new file mode 100644 index 0000000000..46df4e75f5 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/errorhandling/ErrorHandlingTest.java~ @@ -0,0 +1,179 @@ +/* + * 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 ApplicationTest { + + @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"); + + } + +}