It gives us access to all endpoints available in flux and mono controller class
xxxxxxxxxx
@WebFluxTest(controllers = FluxMonoController.class)
public class FluxMonoControllerUnitTest {
@Autowired
WebTestClient webTestClient;
@MockBean
private MoviesInfoService moviesInfoServiceMock;
@Test
void flux() {
webTestClient
.get()
.uri("/flux")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBodyList(Integer.class)
.hasSize(3);
}
@Test
void flux_approach2() {
var flux = webTestClient
.get()
.uri("/flux")
.exchange()
.expectStatus()
.is2xxSuccessful()
.returnResult(Integer.class)
.getResponseBody();
StepVerifier.create(flux)
.expectNext(1, 2, 3)
.expectComplete();
}
@Test
void flux_approach3() {
webTestClient
.get()
.uri("/flux")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBodyList(Integer.class)
.consumeWith(listEntityExchangeResult -> {
var responseBody = listEntityExchangeResult.getResponseBody();
assert (responseBody != null ? responseBody.size() : 0) ==3;
});
}
/**
* Copied from approach 2
*/
@Test
void stream() {
var flux = webTestClient
.get()
.uri("/stream")
.exchange()
.expectStatus()
.is2xxSuccessful()
.returnResult(Integer.class)
.getResponseBody();
StepVerifier.create(flux)
.expectNext(0, 1, 2)
.thenCancel()
.verify();
}
@Test
void mono() {
webTestClient
.get()
.uri("/mono")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody(String.class)
.consumeWith(stringEntityExchangeResult ->{
var response = stringEntityExchangeResult.getResponseBody();
assertEquals("hello-world", response);
});
}
}