gpt4 book ai didi

java - 如何在 spring boot 应用程序状态启动之前让 wiremock 运行?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:38:57 25 4
gpt4 key购买 nike

我有一个 spring boot 微服务的集成测试。问题是该服务在启动时调用外部服务(通过 REST)。我正在使用 WireMock 来模拟调用。 Spring 使应用程序在 WireMock 启动之前启动。因此,其余调用失败,服务也失败。

这个调用是由我们公司也做的图书馆做的,所以我不能在那里改变任何东西。

你对我有什么建议吗?

最佳答案

您可以在测试中创建 WireMockServer 的静态实例。这是一个代码示例:

@RunWith(SpringRunner.class)
@SpringBootTest
public class YourApplicationTests {
static WireMockServer mockHttpServer = new WireMockServer(10000); // endpoint port here

@BeforeClass
public static void setup() throws Exception {
mockHttpServer.stubFor(get(urlPathMatching("/")).willReturn(aResponse().withBody("test").withStatus(200)));
mockHttpServer.start();
}

@AfterClass
public static void teardown() throws Exception {
mockHttpServer.stop();
}

@Test
public void someTest() throws Exception {
// your test code here
}
}

关于java - 如何在 spring boot 应用程序状态启动之前让 wiremock 运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48065941/

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