BAEL-4884 Add unit tests for map vs. flatMap (#10836)

This commit is contained in:
nguyennamthai 2021-05-31 12:00:38 +07:00 committed by GitHub
parent 1c9bccca01
commit 9501c43785
1 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.reactor.mapping;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThat;
public class MappingUnitTest {
@Test
public void givenInputStream_whenCallingTheMapOperator_thenItemsAreTransformed() {
Function<String, String> mapper = String::toUpperCase;
Flux<String> inFlux = Flux.just("baeldung", ".", "com");
Flux<String> outFlux = inFlux.map(mapper);
StepVerifier.create(outFlux)
.expectNext("BAELDUNG", ".", "COM")
.expectComplete()
.verify();
}
@Test
public void givenInputStream_whenCallingTheFlatMapOperator_thenItemsAreFlatten() {
Function<String, Publisher<String>> mapper = s -> Flux.just(s.toUpperCase().split(""));
Flux<String> inFlux = Flux.just("baeldung", ".", "com");
Flux<String> outFlux = inFlux.flatMap(mapper);
List<String> output = new ArrayList<>();
outFlux.subscribe(output::add);
assertThat(output).containsExactlyInAnyOrder("B", "A", "E", "L", "D", "U", "N", "G", ".", "C", "O", "M");
}
}