From b8b4f8677a8c9af54d34fb6c2b1bdc33d7703003 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Sat, 8 Oct 2022 15:23:33 +0200 Subject: [PATCH] BAEL-5813: Mono<> doOnSuccess vs doOnNex --- .../src/main/java/onsuccess/Payment.java | 10 +++ .../main/java/onsuccess/PaymentService.java | 9 +++ .../MonoOnSuccessVsOnNexUnitTest.java | 67 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 spring-5-webflux-2/src/main/java/onsuccess/Payment.java create mode 100644 spring-5-webflux-2/src/main/java/onsuccess/PaymentService.java create mode 100644 spring-5-webflux-2/src/test/java/onsuccess/MonoOnSuccessVsOnNexUnitTest.java diff --git a/spring-5-webflux-2/src/main/java/onsuccess/Payment.java b/spring-5-webflux-2/src/main/java/onsuccess/Payment.java new file mode 100644 index 0000000000..e8e2f79db0 --- /dev/null +++ b/spring-5-webflux-2/src/main/java/onsuccess/Payment.java @@ -0,0 +1,10 @@ +package onsuccess; + + +public class Payment { + private final int amount; + + public Payment(int amount) { + this.amount = amount; + } +} \ No newline at end of file diff --git a/spring-5-webflux-2/src/main/java/onsuccess/PaymentService.java b/spring-5-webflux-2/src/main/java/onsuccess/PaymentService.java new file mode 100644 index 0000000000..6c4885a3e7 --- /dev/null +++ b/spring-5-webflux-2/src/main/java/onsuccess/PaymentService.java @@ -0,0 +1,9 @@ +package onsuccess; + +public class PaymentService { + + public void processPayment(Payment payment) { + System.err.println("Payment processed: " + payment); + } + +} \ No newline at end of file diff --git a/spring-5-webflux-2/src/test/java/onsuccess/MonoOnSuccessVsOnNexUnitTest.java b/spring-5-webflux-2/src/test/java/onsuccess/MonoOnSuccessVsOnNexUnitTest.java new file mode 100644 index 0000000000..1abce7b8a7 --- /dev/null +++ b/spring-5-webflux-2/src/test/java/onsuccess/MonoOnSuccessVsOnNexUnitTest.java @@ -0,0 +1,67 @@ +package onsuccess; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import reactor.core.publisher.Mono; + +class MonoOnSuccessVsOnNexUnitTest { + + @Mock + PaymentService paymentService = mock(PaymentService.class); + + @BeforeEach + void beforeEach() { + reset(paymentService); + } + + @Test + void givenAPaymentMono_whenCallingServiceOnNext_thenCallServiceWithPayment() { + Payment paymentOf100 = new Payment(100); + Mono paymentMono = Mono.just(paymentOf100); + + paymentMono.doOnNext(paymentService::processPayment) + .block(); + + verify(paymentService).processPayment(paymentOf100); + } + + @Test + void givenAnEmptyMono_whenCallingServiceOnNext_thenDoNotCallService() { + Mono emptyMono = Mono.empty(); + + emptyMono.doOnNext(paymentService::processPayment) + .block(); + + verify(paymentService, never()).processPayment(any()); + } + + @Test + void givenAPaymentMono_whenCallingServiceOnSuccess_thenCallServiceWithPayment() { + Payment paymentOf100 = new Payment(100); + Mono paymentMono = Mono.just(paymentOf100); + + paymentMono.doOnSuccess(paymentService::processPayment) + .block(); + + verify(paymentService).processPayment(paymentOf100); + } + + @Test + void givenAnEmptyMono_whenCallingServiceOnSuccess_thenCallServiceWithNull() { + Mono emptyMono = Mono.empty(); + + emptyMono.doOnSuccess(paymentService::processPayment) + .block(); + + verify(paymentService).processPayment(null); + } + +} \ No newline at end of file