WireMock czyli jak wykonać mockowanie Rest Api
WireMock czyli jak wykonać mockowanie Rest Api
WireMock jest biblioteką która umożliwia nam mockowanie/symulowanie wywołań Rest Api. W tym artykule napiszemy test integracyjny w oparciu o Spring Boota i bibliotekę WireMock który zasymuluje nam odpowiedź z zewnętrznego serwisu:
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-standalone</artifactId> <version>2.19.0</version> </dependency>
W pliku application.properties definiujemy przykładowy HOST oraz PORT dla serwisu którego odpowiedź będziemy mockować:
wiremocktest.host = localhost wiremocktest.port = 8888
Klasa konfiguracyjna:
@Configuration @ConfigurationProperties(prefix = "wiremocktest") public class ConfigProperties { private String host; private int port; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
Uruchamiamy WireMockServer:
@BeforeEach public void setup(){ restTemplate = new RestTemplate(); response = null; wireMockServer = new WireMockServer(wireMockConfig().port(configProperties.getPort())); wireMockServer.start(); }
oraz definiujemy test wraz z zasymulowaną odpowiedzią:
@Import(ConfigProperties.class) @EnableConfigurationProperties(value = ConfigProperties.class) @ContextConfiguration(classes = ConfigProperties.class) @TestPropertySource("classpath:application.properties") @ExtendWith(SpringExtension.class) public class WiremockTests { RestTemplate restTemplate; ResponseEntity response; WireMockServer wireMockServer; @Autowired private ConfigProperties configProperties; @BeforeEach public void setup(){ restTemplate = new RestTemplate(); response = null; wireMockServer = new WireMockServer(wireMockConfig().port(configProperties.getPort())); wireMockServer.start(); } @Test public void givenWireMockAdminEndpoint_whenGetWithoutParams_thenVerifyRequest() { RestTemplate restTemplate = new RestTemplate(); response = restTemplate.getForEntity("http://localhost:8888/__admin", String.class); assertThat("Verify Response Body", response.getBody().toString().contains("mappings")); assertThat("Verify Status Code", response.getStatusCode().equals(HttpStatus.OK)); } @Test public void testResourceApi() { System.out.println(configProperties.getHost()); wireMockServer.stubFor(get(urlEqualTo("/api/resource")) .willReturn(aResponse() .withStatus(HttpStatus.OK.value()) .withHeader("Content-Type", TEXT_PLAIN_VALUE) .withBody("test resource"))); response = restTemplate.getForEntity(createHttpLink(), String.class); assertThat("Verify Response Body", response.getBody().toString().contains("test resource")); assertThat("Verify Status Code", response.getStatusCode().equals(HttpStatus.OK)); wireMockServer.stop(); } private String createHttpLink() { StringBuilder httpLink = new StringBuilder(); httpLink.append("http://"); httpLink.append(configProperties.getHost()); httpLink.append(":"); httpLink.append(configProperties.getPort()); httpLink.append("/api/resource"); return httpLink.toString(); } }
Leave a comment