spring - Springboot 测试控制器通过 WebClient 而不是 WebTestClient
问题描述
我正在尝试一些可能被误导的事情,但请帮忙。
我想通过@WebFluxTest
. 但是我想使用WebClient
而不是WebTestClient
. 如何才能做到这一点?
到目前为止,我设法使用反射来ExchangeFunction
摆脱 WebTestClient 并将其分配给 WebClient - 它有效!拨打电话,控制器响应 - 太棒了。但是我认为这不是一个好方法。有没有更好的办法?
谢谢你。
解决方案
理想情况下,您应该使用WebTestClient
更方便的包装器来围绕WebClient
. 就像. TestRestTemplate
_ RestTemplate
两者都允许创建请求,并且您可以轻松地做出断言。它们的存在是为了让您的测试生活更轻松。
如果您真的想使用 aWebClient
而不是 aWebTestClient
并手动执行断言(这意味着您可能会使事情复杂化),您可以使用WebClient.Builder
创建一个。Spring Boot 会自动配置一个,您可以简单地在测试中自动装配它并调用该build
方法。
@SpringBootTest
public void YourTest {
@Autowired
private WebClient.Builder webClientBuilder;
@Test
public void doTest() {
WebClient webClient = webClientBuilder.build();
}
}
同样也应该适用@WebFluxTest
。
推荐阅读
- java - 项目中缺少 Gradle
- python-2.7 - GIMP script-fu,“file-glob”可以只返回具有特定扩展名的文件吗?
- pybind11 - pybind11 def_property 没有在 C++ 中调用设置器
- javascript - JS 映射对象选择嵌套数组键值以使它们成为更高级别的键值
- sql - Doctrine 自定义实体存储库 (QueryBuilder)
- python - 如何在 django 模型字段中写入具有唯一列的多行
- c# - 找不到方法:netcoreapp2.2 中的 GraphQL.ExecutionOptions.set_NameConverter
- ios - UI 不会随着后台 API 调用而更新
- python - 如何处理 Discogs API 响应并将数据提取到列表中
- java - 带有“in”运算符的 Mongo 存储库查询错误