Spring boot 将WebClient与Spring MVC一起使用时要使用的正确依赖项
我正在使用SpringMVC开发一些控制器 我想编写一些场景集成测试,这些测试将涉及调用我的应用程序的多个控制器 通常情况下,我只会在这些测试中使用,但文档说明:Spring boot 将WebClient与Spring MVC一起使用时要使用的正确依赖项,spring-boot,spring-mvc,spring-webclient,Spring Boot,Spring Mvc,Spring Webclient,我正在使用SpringMVC开发一些控制器 我想编写一些场景集成测试,这些测试将涉及调用我的应用程序的多个控制器 通常情况下,我只会在这些测试中使用,但文档说明: 请考虑使用Or.SrpFrrgWorks.Web.ReaServ.clit.WebCube,它拥有更现代的API并支持同步、异步和流式方案。 因此,我想通过使用WebClient编写未来验证代码。但我有几个问题: 这里应该包括哪些依赖项 问题是我是否同时拥有(1)和(2)而没有包含太多内容?是否有一个单独的依赖项,我应该包括专门用于访
请考虑使用Or.SrpFrrgWorks.Web.ReaServ.clit.WebCube,它拥有更现代的API并支持同步、异步和流式方案。 因此,我想通过使用WebClient编写未来验证代码。但我有几个问题:
考虑到我只想在编写的集成测试中向服务器发出HTTP请求,我认为使用WebClient可以吗?还是更愿意在这些测试中使用TestWebClient?最佳实践是什么?测试使用
testrestemplate
将现有集成测试转换为使用WebTestClient
package no.mycompany.myapp.user;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;
@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginControllerTest {
@Autowired
WebTestClient webTestClient;
@Test
public void postLogin_withoutUserCredentials_receiveUnauthorized() {
webTestClient.post()
.uri("/login")
.exchange()
.expectStatus().isUnauthorized();
}
}
将此添加到POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
org.springframework.boot
弹簧启动器webflux
测试
以下内容已在POM中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
测试使用testrest模板将现有集成测试转换为使用WebTestClient
package no.mycompany.myapp.user;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;
@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginControllerTest {
@Autowired
WebTestClient webTestClient;
@Test
public void postLogin_withoutUserCredentials_receiveUnauthorized() {
webTestClient.post()
.uri("/login")
.exchange()
.expectStatus().isUnauthorized();
}
}
将此添加到POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
org.springframework.boot
弹簧启动器webflux
测试
以下内容已在POM中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
WebTestClient包含与WebClient类似的请求方法。此外,它还包含检查响应状态、标头和正文的方法。您还可以在WebTestClient中使用断言库,如AssertJ。WebTestClient包含类似于WebClient的请求方法。此外,它还包含检查响应状态、标头和正文的方法。您还可以在WebTestClient中使用断言库,如AssertJ。谢谢-这很好用。我想使用WebClient alonside MVC需要整个webflux启动器。@vab2048:很高兴听到这个消息。是的,只要从我的答案中删除test
。谢谢-这很有效。我想使用WebClient alonside MVC需要整个webflux启动器。@vab2048:很高兴听到这个消息。是的,只要从我的答案中删除test
。溴