FluxMonoControllerTest.java
2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.krunal.reactive.fluxmonoplayground;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@WebFluxTest
public class FluxMonoControllerTest {
@Autowired
WebTestClient webTestClient;
@Test
public void flux_approach1() {
Flux<Integer> integerFlux = webTestClient.get()
.uri("/flux")
.accept(MediaType.APPLICATION_STREAM_JSON)
.exchange()
.expectStatus().isOk()
.returnResult(Integer.class)
.getResponseBody();
StepVerifier.create(integerFlux)
.expectSubscription()
.expectNext(1, 2, 3, 4, 5, 6)
.verifyComplete();
}
@Test
public void flux_approach2() {
webTestClient.get()
.uri("/flux")
.accept(MediaType.APPLICATION_STREAM_JSON)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_STREAM_JSON)
.expectBodyList(Integer.class)
.hasSize(6);
}
@Test
public void flux_approach3() {
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6);
EntityExchangeResult<List<Integer>> listEntityExchangeResult = webTestClient.get()
.uri("/flux")
.accept(MediaType.APPLICATION_STREAM_JSON)
.exchange()
.expectStatus().isOk()
.expectBodyList(Integer.class)
.returnResult();
assertEquals(integerList, listEntityExchangeResult.getResponseBody());
}
@Test
public void flux_approach4() {
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6);
webTestClient.get()
.uri("/flux")
.accept(MediaType.APPLICATION_STREAM_JSON)
.exchange()
.expectStatus().isOk()
.expectBodyList(Integer.class)
.consumeWith(result -> {
assertEquals(integerList, result.getResponseBody());
});
}
}