Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

import java.io.IOException;
Expand All @@ -18,115 +13,111 @@

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class ApplicationTest {
public class ErrorHandlingIntegrationTest {

@Autowired
private WebTestClient webTestClient;

@Test
public void testLocalErrorHandlingUsingOnErrorReturn() throws IOException {
public void givenErrorReturn_whenUsernamePresent_thenOk() throws IOException {

System.out.println("Testing local error handling using onErrorReturn");

// Pass a username
String i = webTestClient.get()
String s = webTestClient.get()
.uri("/api/endpoint1?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.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")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();

assertEquals("Hello, Stranger", i);

assertEquals("Hello, Stranger", s);
}

@Test
public void testLocalErrorHandlingUsingOnErrorResumeWithFallback() throws IOException {

System.out.println("Testing local error handling using onErrorResume with fallback");
public void givenResumeFallback_whenUsernamePresent_thenOk() throws IOException {

// Pass a username
String i = webTestClient.get()
String s = webTestClient.get()
.uri("/api/endpoint2?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();

assertEquals("Hello, Tony", i);
assertEquals("Hello, Tony", s);
}

// Do not pass a username
i = webTestClient.get()
@Test
public void givenResumeFallback_whenNoUsername_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint2")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();

assertEquals("Hello, Stranger", i);
assertEquals("Hello, Stranger", s);

}

@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");

// Pass a username
String i = webTestClient.get()
String s = webTestClient.get()
.uri("/api/endpoint3?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();

assertEquals("Hello, Tony", i);
assertEquals("Hello, Tony", s);
}

// Do not pass a username
i = webTestClient.get()
@Test
public void givenResumeDynamicValue_whenNoUsername_thenOk() throws IOException {
String s = 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);

assertEquals("Hi, I looked around for your name but found: No value present", s);
}

@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()
@Test
public void givenResumeRethrow_whenUsernamePresent_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint4?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();

assertEquals("Hello, Tony", i);
assertEquals("Hello, Tony", s);
}

@Test
public void givenResumeRethrow_whenNoUsername_thenOk() throws IOException {

// Do not pass a username
webTestClient.get()
.uri("/api/endpoint4")
.accept(MediaType.TEXT_PLAIN)
Expand All @@ -140,26 +131,24 @@ public void testLocalErrorHandlingUsingOnErrorResumeWithCatchAndRethrow() throws
.isNotEmpty()
.jsonPath("$.message")
.isEqualTo("please provide a name");

}

@Test
public void testGlobalErrorHandlingUsingErrorWebExceptionHandler() throws IOException {

System.out.println("Testing local error handling using ErrorWebExceptionHandler");
@Test
public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() throws IOException {

// Pass a username
String i = webTestClient.get()
String s = 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
assertEquals("Hello, Tony", s);
}

@Test
public void givenGlobalErrorHandling_whenNoUsername_thenOk() throws IOException {
webTestClient.get()
.uri("/api/endpoint5")
.accept(MediaType.TEXT_PLAIN)
Expand All @@ -175,5 +164,5 @@ public void testGlobalErrorHandlingUsingErrorWebExceptionHandler() throws IOExce
.isEqualTo("please provide a name");

}

}

This file was deleted.