Skip to content

Commit 2f70c5a

Browse files
authored
BAEL-7567 - How to convert List<T> to Flux<T> in Spring Reactor (#17106)
* BAEL-7567 - How to convert List<T> to Flux<T> in Spring Reactor (Adding Test cases) * BAEL-7567 - How to convert List<T> to Flux<T> in Spring Reactor (modify test cases) * BAEL-7567 - How to convert List<T> to Flux<T> in Spring Reactor (modify test cases)
1 parent be8a5e6 commit 2f70c5a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.reactor.convertlistoflux;
2+
3+
import java.util.List;
4+
import org.junit.jupiter.api.Test;
5+
import reactor.core.publisher.Flux;
6+
import reactor.test.StepVerifier;
7+
8+
public class ListToFluxUnitTest {
9+
10+
@Test
11+
public void givenList_whenCallingFromIterableOperator_thenListItemsTransformedAsFluxAndEmitted(){
12+
13+
List<Integer> list = List.of(1, 2,3);
14+
Flux<Integer> flux = listToFluxUsingFromIterableOperator(list);
15+
16+
StepVerifier.create(flux)
17+
.expectNext(1)
18+
.expectNext(2)
19+
.expectNext(3)
20+
.expectComplete()
21+
.verify();
22+
}
23+
24+
@Test
25+
public void givenList_whenCallingCreateOperator_thenListItemsTransformedAsFluxAndEmitted(){
26+
27+
List<Integer> list = List.of(1, 2,3);
28+
Flux<Integer> flux = listToFluxUsingCreateOperator(list);
29+
30+
StepVerifier.create(flux)
31+
.expectNext(1)
32+
.expectNext(2)
33+
.expectNext(3)
34+
.expectComplete()
35+
.verify();
36+
}
37+
38+
private <T> Flux<T> listToFluxUsingFromIterableOperator(List<T> list) {
39+
return Flux
40+
.fromIterable(list)
41+
.log();
42+
}
43+
44+
private <T> Flux<T> listToFluxUsingCreateOperator(List<T> list) {
45+
return Flux.create(sink-> {
46+
list.forEach(sink::next);
47+
sink.complete();
48+
});
49+
}
50+
51+
}

0 commit comments

Comments
 (0)