Merge pull request #12838 from etrandafir93/features/BAEL-5813-mono_onSuccess_vs_onNext

BAEL-5813: Mono<> doOnSuccess vs doOnNex
This commit is contained in:
davidmartinezbarua 2022-10-10 15:57:54 -03:00 committed by GitHub
commit 5a68e4459d
3 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package onsuccess;
public class Payment {
private final int amount;
public Payment(int amount) {
this.amount = amount;
}
}

View File

@ -0,0 +1,9 @@
package onsuccess;
public class PaymentService {
public void processPayment(Payment payment) {
System.err.println("Payment processed: " + payment);
}
}

View File

@ -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<Payment> paymentMono = Mono.just(paymentOf100);
paymentMono.doOnNext(paymentService::processPayment)
.block();
verify(paymentService).processPayment(paymentOf100);
}
@Test
void givenAnEmptyMono_whenCallingServiceOnNext_thenDoNotCallService() {
Mono<Payment> emptyMono = Mono.empty();
emptyMono.doOnNext(paymentService::processPayment)
.block();
verify(paymentService, never()).processPayment(any());
}
@Test
void givenAPaymentMono_whenCallingServiceOnSuccess_thenCallServiceWithPayment() {
Payment paymentOf100 = new Payment(100);
Mono<Payment> paymentMono = Mono.just(paymentOf100);
paymentMono.doOnSuccess(paymentService::processPayment)
.block();
verify(paymentService).processPayment(paymentOf100);
}
@Test
void givenAnEmptyMono_whenCallingServiceOnSuccess_thenCallServiceWithNull() {
Mono<Payment> emptyMono = Mono.empty();
emptyMono.doOnSuccess(paymentService::processPayment)
.block();
verify(paymentService).processPayment(null);
}
}