简体   繁体   English

使用 okhttp 和新的 Spring 6 http 接口进行测试

[英]Testing with okhttp and new Spring 6 http interfaces

What's the best way to write integration tests that uses the new Spring 6 http interfaces with a mock server?编写使用新的 Spring 6 http 接口和模拟服务器的集成测试的最佳方法是什么? Example:例子:

@Bean
Blah configService() {

    var client = WebClient.builder().baseUrl(baseUrl)
            .defaultStatusHandler(HttpStatusCode::is4xxClientError, resp -> Mono.empty())
            .build();
    var proxyFactory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
    return proxyFactory.createClient(Blah.class);
}
interface BlahService {
    @GetExchange("/ok")
    ResponseEntity<Blah> getBlah();
}

basically trying to discover a reasonable way to use the mock server from okhttp ( https://github.com/spring-projects/spring-framework/blob/main/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java )基本上是试图从 okhttp ( https://github.com/spring-projects/spring-framework/blob/main/spring-webflux/src/test/java/org/springframework/web /reactive/function/client/WebClientIntegrationTests.java )

with the server.url as a property I can inject the value and use it in test env as base url in my WebClient config.使用server.url作为属性,我可以注入该值并在测试环境中使用它作为我的 WebClient 配置中的基础 url。

Thanks.谢谢。

I can't find anything specific on the web on this topic yet.我还找不到有关此主题的 web 的任何具体信息。

You will need to use @DynamicPropertySource to inject that property in your test class like so您将需要使用@DynamicPropertySource像这样在您的测试 class 中注入该属性

@DynamicPropertySource
static void properties(DynamicPropertyRegistry registry) {
    registry.add("server.url", () -> "http://localhost:" + mockWebServer.getPort());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM