Şöyle yaparız
@Rulepublic WireMockRule wireMockRule = new WireMockRule(int port);
Açıklaması şöyle
We can integrate a WireMock server into JUnit test cases by using the @Rule annotation. This allows JUnit to manage the life cycle, starting the server prior to each test method and stopping it after the method returns.Similar to the programmatically managed server, a JUnit managed WireMock server can be created as a Java object with the given port number:If no arguments are supplied, server port will take the default value, 8080. Server host, defaulting to localhost, and other configurations may be specified using the Options interface.
dropConnectionPercentage - network failures
ÖrnekElimizde şöyle bir kod olsun
@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class NetworkFailureTest { @Autowired private TestRestTemplate restTemplate; @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig() .dynamicPort() .networkLatency(1000, TimeUnit.MILLISECONDS) .dropConnectionPercentage(50)); ... }
Testleri şöyle yaparız
@Testpublic void testGreeting() { String response = restTemplate.getForObject("/greeting", String.class); assertEquals("Hello, world!", response); } @Test public void testTimeout() { wireMockRule.stubFor(get(urlEqualTo("/greeting")) .willReturn(aResponse() .withStatus(200) .withFixedDelay(2000))); assertThrows(RestClientException.class, () -> { restTemplate.getForObject("/greeting", String.class); }); }
shutdownServer metodu
Şöyle yaparız
@Testpublic void testConnectionError() { wireMockRule.shutdownServer(); assertThrows(RestClientException.class, () -> { restTemplate.getForObject("/greeting", String.class); }); }
withFault metodu - CONNECTION ERROR
Örnek
Şöyle yaparız
wireMockRule.stubFor(get(urlEqualTo("/api/data")) .willReturn(aResponse() .withFault(Fault.CONNECTION_RESET_BY_PEER)));
withStatus metodu - HTTP Errors
Örnek
Şöyle yaparız
wireMockRule.stubFor(get(urlEqualTo("/api/data")) .willReturn(aResponse() .withStatus(500)));
Hiç yorum yok:
Yorum Gönder