> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/43.-testing/43.3-testing-spring-boot-applications/43.3.9-auto-configured-spring-webflux-tests.md).

# 43.3.9 自动配置的Spring WebFlux测试

你可以使用`@WebFluxTest`检测[Spring WebFlux](https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference//web-reactive.html)控制器是否工作正常。该注解将自动配置Spring WebFlux设施，并且只扫描注解`@Controller`、`@ControllerAdvice`、`@JsonComponent`、`Converter`、`GenericConverter`、`Filter`和`WebFluxConfigurer`的bean。其他常规的`@Component` bean将不会被扫描。

**注** 如果你需要注册额外的组件，比如Jackson`模块`，你可以在你的测试上使用`@Import`来导入另外的配置类。

通常`@WebFluxTest`只限于单个控制器（controller）使用，并结合`@MockBean`以提供需要的协作者（collaborators）的mock实现。

`@WebFluxTest`也会自动配置[`WebTestClient`](https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference/testing.html#webtestclient)。WebTestClient为快速测试WebFlux控制器提供了一种强大的方式，并且不需要启动一个完整的HTTP服务器。

**注** 使用`@AutoConfigureWebTestClient`注解一个non-`@WebFluxTest`的类（比如`@SpringBootTest`）也可以自动配置`WebTestClient`。下面的例子展示了一个同时使用`@WebFluxTest`和`WebTestClient`的类：

```java
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.WebTestClient;

@RunWith(SpringRunner.class)
@WebFluxTest(UserVehicleController.class)
public class MyControllerTests {

    @Autowired
    private WebTestClient webClient;

    @MockBean
    private UserVehicleService userVehicleService;

    @Test
    public void testExample() throws Exception {
        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");
    }

}
```

在[附录](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#test-auto-configuration)中可以查看`@WebFluxTest`开启的自动配置列表。

**注** 有时，光光编写Spring MVC测试是不够的。Spring Boot可以帮助你[在实际的服务器上运行完整的端到端的测试](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-with-running-server)。
