Spring Boot 中是否有任何属性可用于配置 @Autowired WebTestClient?例如,如何在 WebTestClient 上设置 servlet 上下文路径(或者只是一些基本路径)?
现在我的网络测试是这样配置的:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class MyTestClass{
@Autowired
private WebTestClient cl;
//the rest of it
}
换句话说,什么是 Spring Boot 等同于
WebTestClient client = WebTestClient.bindToServer()
.baseUrl("http://localhost:<random port>/myServletContext").build();
我没有在文档中找到任何有用的信息: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
最佳答案
使用当前应用程序上下文构建 webTestClient,无需硬编码 uri 和端口号
@Autowired
ApplicationContext context;
@Autowired
WebTestClient webTestClient;
@Before
public void setup() throws Exception {
this.webTestClient = WebTestClient.bindToApplicationContext(this.context).build();
}
关于 Spring Boot : how to configure autowired WebTestClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56472500/