package com.qa.utilities; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import org.testng.annotations.Test; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import com.qa.utilities.Util; import io.restassured.RestAssured; import io.restassured.response.Response; public class WireMockTest { String MOCKED_URL = "http://localhost:8089/api/f1/drivers.json"; String driverId; Util util = new Util(); @Test public void wireMockTest() { WireMockServer wireMockServer = new WireMockServer(options().port(8089)); wireMockServer.start(); // checking mock server started Response response = RestAssured.get(MOCKED_URL); // status code should be 404 as mock server not started int mockStatusCode = response.getStatusCode(); System.out.println("Mock status code is : " + mockStatusCode); // call method to mock response sync(); } public Response sync() { // to get response from real url // Response responseFromRealUrl = // RestAssured.get("BASE_URL/api/f1/drivers.json"); Response responseFromRealUrl = util.get_driver(driverId); int statuscodeFromRealUrl = responseFromRealUrl.getStatusCode(); // System.out.println(statuscodeFromRealUrl); String dataFromRealUrl = responseFromRealUrl.getBody().asString(); configureFor("localhost", 8089); stubFor(get(urlEqualTo("/api/f1/drivers.json")) .willReturn(aResponse().withStatus(statuscodeFromRealUrl).withBody(dataFromRealUrl))); WireMock.saveAllMappings(); return RestAssured.get(MOCKED_URL); } }