简体   繁体   中英

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? 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 )

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.

Thanks.

I can't find anything specific on the web on this topic yet.

You will need to use @DynamicPropertySource to inject that property in your test class like so

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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