Skip to content

Instantly share code, notes, and snippets.

@pgilad
Created February 17, 2019 19:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgilad/bac8a59e449dc470f3fc5954dd23761a to your computer and use it in GitHub Desktop.
Save pgilad/bac8a59e449dc470f3fc5954dd23761a to your computer and use it in GitHub Desktop.
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