Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example on how to use WebFluxTest to test a Spring Boot WebFlux Controller
package com.blazemeter.dagger.domains.aggregate;
import com.blazemeter.dagger.domains.aggregate.port.AggregateRepository;
import com.blazemeter.dagger.domains.common.CollectionService;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
@WithMockUser
@ExtendWith(SpringExtension.class)
@WebFluxTest(controllers = AggregateController.class)
@Import(CollectionService.class)
class AggregateControllerTest {
@MockBean
AggregateRepository repository;
@Autowired
private WebTestClient client;
@Test
void getIsDataReady() {
Mockito
.when(repository.getIsDataReady(ArgumentMatchers.any()))
.thenReturn(Mono.just(true));
client.get()
.uri("/api/aggregate/data-ready?masterId=" + RandomUtils.nextInt())
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.