From 93637134e9ac0cc8d84eafd02465779b444d6324 Mon Sep 17 00:00:00 2001 From: Nguyen Nam Thai Date: Sun, 1 Aug 2021 23:56:06 +0700 Subject: [PATCH 1/3] BAEL-4883 Exception handling in Project Reactor --- .../reactor/exception/ExceptionUnitTest.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java diff --git a/reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java b/reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java new file mode 100644 index 0000000000..f4da2e325a --- /dev/null +++ b/reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java @@ -0,0 +1,89 @@ +package com.baeldung.reactor.exception; + +import org.junit.Test; +import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.core.publisher.SynchronousSink; +import reactor.test.StepVerifier; + +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class ExceptionUnitTest { + @Test + public void givenInputStreamWithAnInvalidElement_whenAPipelineOperatorThrowsAnException_thenAnErrorIsSentDownstream() { + Function mapper = input -> { + if (input.matches("\\D")) { + throw new NumberFormatException(); + } else { + return Integer.parseInt(input); + } + }; + + Flux inFlux = Flux.just("1", "1.5", "2"); + Flux outFlux = inFlux.map(mapper); + + StepVerifier.create(outFlux) + .expectNext(1) + .expectError(NumberFormatException.class) + .verify(); + } + + @Test + public void givenInputStreamWithAnInvalidElement_whenTheHandleOperatorCallsSinkErrorMethod_thenAnErrorIsSentDownstream() { + BiConsumer> handler = (input, sink) -> { + if (input.matches("\\D")) { + sink.error(new NumberFormatException()); + } else { + sink.next(Integer.parseInt(input)); + } + }; + + Flux inFlux = Flux.just("1", "1.5", "2"); + Flux outFlux = inFlux.handle(handler); + + StepVerifier.create(outFlux) + .expectNext(1) + .expectError(NumberFormatException.class) + .verify(); + } + + @Test + public void givenInputStreamWithAnInvalidElement_whenTheFlatMapOperatorCallsMonoErrorMethod_thenAnErrorIsSentDownstream() { + Function> mapper = input -> { + if (input.matches("\\D")) { + return Mono.error(new NumberFormatException()); + } else { + return Mono.just(Integer.parseInt(input)); + } + }; + + Flux inFlux = Flux.just("1", "1.5", "2"); + Flux outFlux = inFlux.flatMap(mapper); + + StepVerifier.create(outFlux) + .expectNext(1) + .expectError(NumberFormatException.class) + .verify(); + } + + @Test + public void givenInputStreamWithANullElement_whenAPipelineOperatorIsCalled_thenAnNpeIsSentDownstream() { + Function mapper = input -> { + if (input == null) { + return 0; + } else { + return Integer.parseInt(input); + } + }; + + Flux inFlux = Flux.just("1", null, "2"); + Flux outFlux = inFlux.map(mapper); + + StepVerifier.create(outFlux) + .expectNext(1) + .expectError(NullPointerException.class) + .verify(); + } +} From 58b12b04f20123623bc59db95629f3c58ba3837e Mon Sep 17 00:00:00 2001 From: Nguyen Nam Thai Date: Thu, 12 Aug 2021 12:46:15 +0700 Subject: [PATCH 2/3] BAEL-4883 Update Reactor version --- reactor-core/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 6bd725b3b5..c13dda63d5 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -41,8 +41,8 @@ - 3.3.9.RELEASE + 3.4.9 3.6.1 - \ No newline at end of file + From ab3fc12268f9367a94226ac920507afac0d6eb4f Mon Sep 17 00:00:00 2001 From: Nguyen Nam Thai Date: Sun, 15 Aug 2021 23:03:32 +0700 Subject: [PATCH 3/3] BAEL-4883 Shorten test method names --- .../com/baeldung/reactor/exception/ExceptionUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java b/reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java index f4da2e325a..d1d2b3c4d6 100644 --- a/reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java +++ b/reactor-core/src/test/java/com/baeldung/reactor/exception/ExceptionUnitTest.java @@ -12,7 +12,7 @@ import java.util.function.Function; public class ExceptionUnitTest { @Test - public void givenInputStreamWithAnInvalidElement_whenAPipelineOperatorThrowsAnException_thenAnErrorIsSentDownstream() { + public void givenInvalidElement_whenPipelineThrowsException_thenErrorIsSentDownstream() { Function mapper = input -> { if (input.matches("\\D")) { throw new NumberFormatException(); @@ -31,7 +31,7 @@ public class ExceptionUnitTest { } @Test - public void givenInputStreamWithAnInvalidElement_whenTheHandleOperatorCallsSinkErrorMethod_thenAnErrorIsSentDownstream() { + public void givenInvalidElement_whenHandleCallsSinkErrorMethod_thenErrorIsSentDownstream() { BiConsumer> handler = (input, sink) -> { if (input.matches("\\D")) { sink.error(new NumberFormatException()); @@ -50,7 +50,7 @@ public class ExceptionUnitTest { } @Test - public void givenInputStreamWithAnInvalidElement_whenTheFlatMapOperatorCallsMonoErrorMethod_thenAnErrorIsSentDownstream() { + public void givenInvalidElement_whenFlatMapCallsMonoErrorMethod_thenErrorIsSentDownstream() { Function> mapper = input -> { if (input.matches("\\D")) { return Mono.error(new NumberFormatException()); @@ -69,7 +69,7 @@ public class ExceptionUnitTest { } @Test - public void givenInputStreamWithANullElement_whenAPipelineOperatorIsCalled_thenAnNpeIsSentDownstream() { + public void givenNullElement_whenPipelineOperatorExecutes_thenNpeIsSentDownstream() { Function mapper = input -> { if (input == null) { return 0;