Spring Boot : how to configure autowired WebTestClient

标签 spring spring-boot testing integration-testing

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/

相关文章:

spring - 在 MyBatis/Spring 中使用自定义类型

spring - Spring Webflux 安全中的角色层次结构

PHPUnit 将模拟对象作为参数传递给另一个对象的构造函数并使用 expects() 方法

java - 程序中途掉网测试效果

java - 多模块项目中的 spring boot gradle 插件-查找 mainClass 时出错

java - 带有应用程序/八位字节流的spring boot multipartFile抛出异常

java - 使用 swagger 记录枚举值中的字符串

java - 在外部文件夹 war 文件命令中运行类路径 Spring java

java - 解析模板时出错 "welcome",模板可能不存在或无法由任何已配置的模板解析器访问

python - 使用 Tox 复制 setup.py 依赖项