-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
28df938
commit 13343c3
Showing
5 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package usecases.cucumber; | ||
|
||
public class CucumberConstants { | ||
public static final String WIREMOCK_SERVER_NAME = "my-wiremock-server"; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/usecases/cucumber/CucumberSpringConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package usecases.cucumber; | ||
|
||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
import io.cucumber.spring.CucumberContextConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.wiremock.spring.ConfigureWireMock; | ||
import org.wiremock.spring.EnableWireMock; | ||
|
||
@CucumberContextConfiguration | ||
@SpringBootTest(webEnvironment = RANDOM_PORT) | ||
@EnableWireMock( | ||
@ConfigureWireMock(name = CucumberConstants.WIREMOCK_SERVER_NAME, registerSpringBean = true)) | ||
public class CucumberSpringConfiguration {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package usecases.cucumber; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
import com.github.tomakehurst.wiremock.stubbing.StubMapping; | ||
import io.cucumber.java.Before; | ||
import io.cucumber.java.en.Given; | ||
import io.cucumber.java.en.Then; | ||
import io.cucumber.java.en.When; | ||
import io.restassured.RestAssured; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
|
||
public class Steps { | ||
|
||
@Qualifier(CucumberConstants.WIREMOCK_SERVER_NAME) | ||
@Autowired | ||
private WireMockServer wireMockServer; | ||
|
||
private ExtractableResponse<Response> actualResponse; | ||
|
||
@Before | ||
public void beforeEach() { | ||
wireMockServer.resetAll(); | ||
RestAssured.baseURI = "http://localhost:" + wireMockServer.port(); | ||
} | ||
|
||
@Given("^WireMock has endpint (.*)") | ||
public void wireMockHasEndpoint(String endpoint) { | ||
StubMapping okResponse = | ||
WireMock.any(WireMock.urlEqualTo("/" + endpoint)).willReturn(WireMock.status(200)).build(); | ||
wireMockServer.addStubMapping(okResponse); | ||
} | ||
|
||
@When("^WireMock is invoked with (.*)") | ||
public void wireMockIsInvokedWith(String endpoint) { | ||
actualResponse = RestAssured.when().get("/" + endpoint).then().extract(); | ||
} | ||
|
||
@Then("^it should respond (.*)") | ||
public void isShouldResponsWith(int status) { | ||
assertThat(actualResponse.statusCode()).isEqualTo(status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Feature: Test that features can be used | ||
|
||
Scenario: Setup WireMock in Given and try it out in then | ||
Given WireMock has endpint ping | ||
When WireMock is invoked with ping | ||
Then it should respond 200 | ||
When WireMock is invoked with pang | ||
Then it should respond 404 |