跳到主要内容

使用 WebTestClient 进行 WebFlux 测试

DeepSeek V3 中英对照 WebFlux with WebTestClient

你可以使用 WebTestClient 来处理 WebFlux。以下清单展示了如何将 WebTestClient 配置为测试模式:

<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<testMode>WEBTESTCLIENT</testMode>
</configuration>
</plugin>
contracts {
testMode = 'WEBTESTCLIENT'
}

以下示例展示了如何为 WebFlux 设置 WebTestClient 基类和 RestAssured:

import io.restassured.module.webtestclient.RestAssuredWebTestClient;
import org.junit.Before;

public abstract class BeerRestBase {

@Before
public void setup() {
RestAssuredWebTestClient.standaloneSetup(
new ProducerController(personToCheck -> personToCheck.age >= 20));
}
}
}
提示

WebTestClient 模式比 EXPLICIT 模式更快。