Auto-configured Spring WebFlux Tests
To test that Spring WebFlux controllers are working as expected, you can use the @WebFluxTest annotation.
@WebFluxTest auto-configures the Spring WebFlux infrastructure and limits scanned beans to @Controller, @ControllerAdvice, @JsonComponent, Converter, GenericConverter, WebFilter, and WebFluxConfigurer.
Regular @Component and @ConfigurationProperties beans are not scanned when the @WebFluxTest annotation is used.
@EnableConfigurationProperties can be used to include @ConfigurationProperties beans.
| A list of the auto-configurations that are enabled by @WebFluxTestcan be found in the appendix. | 
| If you need to register extra components, such as Jackson Module, you can import additional configuration classes using@Importon your test. | 
Often, @WebFluxTest is limited to a single controller and used in combination with the @MockBean annotation to provide mock implementations for required collaborators.
@WebFluxTest also auto-configures WebTestClient, which offers a powerful way to quickly test WebFlux controllers without needing to start a full HTTP server.
| You can also auto-configure WebTestClientin a non-@WebFluxTest(such as@SpringBootTest) by annotating it with@AutoConfigureWebTestClient.
The following example shows a class that uses both@WebFluxTestand aWebTestClient: | 
- 
Java 
- 
Kotlin 
import org.junit.jupiter.api.Test;
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.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.mockito.BDDMockito.given;
@WebFluxTest(UserVehicleController.class)
class MyControllerTests {
	@Autowired
	private WebTestClient webClient;
	@MockBean
	private UserVehicleService userVehicleService;
	@Test
	void testExample() {
		given(this.userVehicleService.getVehicleDetails("sboot"))
			.willReturn(new VehicleDetails("Honda", "Civic"));
		this.webClient.get().uri("/sboot/vehicle").accept(MediaType.TEXT_PLAIN).exchange()
			.expectStatus().isOk()
			.expectBody(String.class).isEqualTo("Honda Civic");
	}
}import org.junit.jupiter.api.Test
import org.mockito.BDDMockito.given
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.http.MediaType
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.reactive.server.expectBody
@WebFluxTest(UserVehicleController::class)
class MyControllerTests(@Autowired val webClient: WebTestClient) {
	@MockBean
	lateinit var userVehicleService: UserVehicleService
	@Test
	fun testExample() {
		given(userVehicleService.getVehicleDetails("sboot"))
			.willReturn(VehicleDetails("Honda", "Civic"))
		webClient.get().uri("/sboot/vehicle").accept(MediaType.TEXT_PLAIN).exchange()
			.expectStatus().isOk
			.expectBody<String>().isEqualTo("Honda Civic")
	}
}| This setup is only supported by WebFlux applications as using WebTestClientin a mocked web application only works with WebFlux at the moment. | 
| @WebFluxTestcannot detect routes registered through the functional web framework.
For testingRouterFunctionbeans in the context, consider importing yourRouterFunctionyourself by using@Importor by using@SpringBootTest. | 
| @WebFluxTestcannot detect custom security configuration registered as a@Beanof typeSecurityWebFilterChain.
To include that in your test, you will need to import the configuration that registers the bean by using@Importor by using@SpringBootTest. | 
| Sometimes writing Spring WebFlux tests is not enough; Spring Boot can help you run full end-to-end tests with an actual server. |