|
| 1 | +package usecases.cucumber; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 7 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 8 | +import com.github.tomakehurst.wiremock.stubbing.StubMapping; |
| 9 | +import io.cucumber.java.Before; |
| 10 | +import io.cucumber.java.en.Given; |
| 11 | +import io.cucumber.java.en.Then; |
| 12 | +import io.cucumber.java.en.When; |
| 13 | +import io.restassured.RestAssured; |
| 14 | +import io.restassured.response.ExtractableResponse; |
| 15 | +import io.restassured.response.Response; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 18 | + |
| 19 | +public class Steps { |
| 20 | + |
| 21 | + @Qualifier(CucumberConstants.WIREMOCK_SERVER_NAME) |
| 22 | + @Autowired |
| 23 | + private WireMockServer wireMockServer; |
| 24 | + |
| 25 | + private ExtractableResponse<Response> actualResponse; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void beforeEach() { |
| 29 | + wireMockServer.resetAll(); |
| 30 | + RestAssured.baseURI = "http://localhost:" + wireMockServer.port(); |
| 31 | + } |
| 32 | + |
| 33 | + @Given("^WireMock has endpint (.*)") |
| 34 | + public void wireMockHasEndpoint(String endpoint) { |
| 35 | + StubMapping okResponse = |
| 36 | + WireMock.any(WireMock.urlEqualTo("/" + endpoint)).willReturn(WireMock.status(200)).build(); |
| 37 | + wireMockServer.addStubMapping(okResponse); |
| 38 | + } |
| 39 | + |
| 40 | + @When("^WireMock is invoked with (.*)") |
| 41 | + public void wireMockIsInvokedWith(String endpoint) { |
| 42 | + actualResponse = RestAssured.when().get("/" + endpoint).then().extract(); |
| 43 | + } |
| 44 | + |
| 45 | + @Then("^it should respond (.*)") |
| 46 | + public void isShouldResponsWith(int status) { |
| 47 | + assertThat(actualResponse.statusCode()).isEqualTo(status); |
| 48 | + } |
| 49 | +} |
0 commit comments