28 Mart 2023 Salı

WireMock WireMockServer Slow Response

Örnek
Elimizde şöyle bir kod olsun
public class WireMockConfig {
  private WireMockServer wireMockServer;

  public void start() {
    wireMockServer = new WireMockServer(options().port(8080));
    wireMockServer.start();
    configureFor("localhost", 8080);
  }
  public void stop() {
    wireMockServer.stop();
  }
  public void reset() {
    WireMock.reset();
  }
  // Fixed delay
  public void stubSlowResponse() {
    stubFor(get(urlEqualTo("/slow-response"))
                .willReturn(aResponse()
                        .withFixedDelay(5000)
                        .withStatus(200)));
  }
  // Dynamic Delay
  public void stubDynamicDelayResponse() {
    stubFor(get(urlEqualTo("/dynamic-delay-response"))
            .willReturn(aResponse()
                    .withUniformRandomDelay(1000, 5000)
                    .withStatus(200)));
  }
  // Delay distribution
  public void stubLogNormalDelayResponse() {
    stubFor(get(urlEqualTo("/log-normal-delay-response"))
            .willReturn(aResponse()
                    .withLogNormalDistribution(5000, 1.0)
                    .withStatus(200)));
  }
  // Delay sequence
  public void stubDelaySequenceResponse() {
    stubFor(get(urlEqualTo("/delay-sequence-response"))
            .willReturn(aResponse()
                    .withDelaySequence(1000, 2000, 3000, 4000, 5000)
                    .withStatus(200)));
  }
}
Testte şöyle yaparız
@RunWith(SpringRunner.class)
@WebMvcTest(SlowResponseController.class) public class SlowResponseControllerTest { @Autowired private MockMvc mockMvc; @MockBean private RestTemplate restTemplate; @Rule public WireMockRule wireMockRule = new WireMockRule(8080); @Autowired private WireMockConfig wireMockConfig; @Before public void setup() { wireMockConfig.start(); } @Test public void should_return_slow_response() throws Exception { wireMockConfig.stubSlowResponse(); mockMvc.perform(MockMvcRequestBuilders.get("/slow-response")) .andExpect(MockMvcResultMatchers.status().isOk()); verify(getRequestedFor(urlEqualTo("/slow-response"))); } }



Hiç yorum yok:

Yorum Gönder