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