2
我试图使用Wiremock来拦截在ApplicationReadyEvent
上触发的HTTP调用。问题在于,此调用是在应用Wiremock规则之前完成的。所以这个API没有被嘲笑。如何模拟在ApplicationReadyEvent上触发的HTTP调用
参见下面的示例
public class OnApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent>{
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// in this pathe is being send a request
consulHealthCheckService.register();
}
}
的磕碰在@Before
相位,这实际上是触发一次Servlet容器是准备进行配置。因此,实际上 - HTTP调用被激发
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ConsulApplication.class)
public class ConsulApplicationTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(Agent.DEFAULT_PORT);
@Before
public void setup(){
// is being executed after the http call was fired
stubFor(get(anyUrl()).willReturn(aResponse().withStatus(HttpStatus.OK.value())));
stubFor(put(urlEqualTo("/agent/service/register"))
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", APPLICATION_JSON_VALUE)
));
}
@Test
public void shouldRegisterServiceOnApplicationStartup(){
verify(putRequestedFor(urlEqualTo("/agent/service/register")));
}
}
有什么办法如何存根给定的HTTP调用之后?请注意:Iam不能模拟实际触发呼叫的服务代码。