在运行 springboot 测试时,在 bean 的
@PostConstruct
中进行 REST 调用,我希望在应用程序上下文完全加载之前让我的 wiremockServer 运行。有没有办法确保首先发生这种情况?
您可以在测试中创建 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
}
}