Springboot 测试在上下文之前初始化 wiremock

问题描述 投票:0回答:1

在运行 springboot 测试时,在 bean 的

@PostConstruct
中进行 REST 调用,我希望在应用程序上下文完全加载之前让我的 wiremockServer 运行。有没有办法确保首先发生这种情况?

spring-boot junit wiremock
1个回答
0
投票

您可以在测试中创建 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
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.