gpt4 book ai didi

java - JUnit测试中如何将MockWebServer端口设置为WebClient?

转载 作者:行者123 更新时间:2023-11-30 12:04:20 24 4
gpt4 key购买 nike

我将 spring-bootWebClient 一起使用,它作为 bean Autowiring 。

问题:在编写junit 集成测试时,我必须使用okhttp MockWebServer。这个模拟总是在一个随机端口上启动,例如 localhost:14321

现在我的 WebClient 当然有一个固定的 url,它将请求发送到。此 url 可能由 application.properties 参数提供,例如 webclient.url=https://my.domain.com/,因此我可以在 junit 测试。但只是静态的。

问题:如何重置 @SpringBootTest 中的 WebClient bean,以便它始终将请求发送到我的模拟服务器?

@Service
public class WebClientService {
public WebClientService(WebClient.Builder builder, @Value("${webclient.url}" String url) {
this.webClient = builder.baseUrl(url)...build();
}

public Response send() {
return webClient.body().exchange().bodyToMono();
}
}

@Service
public void CallingService {
@Autowired
private WebClientService service;

public void call() {
service.send();
}
}


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyWebTest {
@Autowired
private CallingService calling;

@Test
public void test() {
MockWebServer mockWebServer = new MockWebServer();
System.out.println("Current mock server url: " + mockWebServer.url("/").toString()); //this is random

mockWebServer.enqueue(new MockResponse()....);

//TODO how to make the mocked server url public to the WebClient?
calling.call();
}
}

如您所见,我正在编写完整的真实世界 junit 集成测试。唯一的问题是:如何将 MockWebServer url 和端口传递给 WebClient,以便它自动将请求发送到我的 mock??

旁注:我绝对需要在 MockWebServer 中使用一个随机端口,以免干扰其他正在运行的测试或应用程序。因此必须坚持使用随机端口,并找到一种方法将其传递给 webclient(或动态覆盖应用程序属性)。


更新:我想出了以下方法,它有效。但也许有人知道如何使模拟服务器字段非静态

@ContextConfiguration(initializers = RandomPortInitializer.class)
public abstract class AbstractITest {
@ClassRule
public static final MockWebServer mockWebServer = new MockWebServer();

public static class RandomPortInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(applicationContext,
"webclient.url=" + mockWebServer.url("/").toString());
}
}
}

最佳答案

从 Spring Framework 5.2.5 (Spring Boot 2.x) 开始,您可以使用非常方便的 DynamicPropertySource 注解。

这是一个完整的示例,您可以如何将它与 MockWebServer 一起使用以绑定(bind)正确的端口:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class AbstractIT {

static MockWebServer mockWebServer;

@DynamicPropertySource
static void properties(DynamicPropertyRegistry r) throws IOException {
r.add("some-service.url", () -> "http://localhost:" + mockWebServer.getPort());
}

@BeforeAll
static void beforeAll() throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();
}

@AfterAll
static void afterAll() throws IOException {
mockWebServer.shutdown();
}
}

关于java - JUnit测试中如何将MockWebServer端口设置为WebClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com