-
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.
Merge pull request #77 from wiremock/feature/issue-73-register-spring…
…-bean feat: register WireMockServer as a Spring Bean (refs #73)
- Loading branch information
Showing
9 changed files
with
140 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
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
33 changes: 33 additions & 0 deletions
33
src/test/java/usecases/AutowireNamedWireMockServerTest.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,33 @@ | ||
package usecases; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import io.restassured.RestAssured; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.wiremock.spring.ConfigureWireMock; | ||
import org.wiremock.spring.EnableWireMock; | ||
|
||
@SpringBootTest | ||
@EnableWireMock(@ConfigureWireMock(name = "mywiremock", registerSpringBean = true)) | ||
class AutowireNamedWireMockServerTest { | ||
|
||
@Qualifier("mywiremock") | ||
@Autowired | ||
private WireMockServer wireMockServer; | ||
|
||
@Value("${wiremock.server.baseUrl}") | ||
private String wiremockUrl; | ||
|
||
@Test | ||
void returnsTodos() { | ||
this.wireMockServer.stubFor(get("/ping").willReturn(aResponse().withStatus(200))); | ||
|
||
RestAssured.when().get(this.wiremockUrl + "/ping").then().statusCode(200); | ||
} | ||
} |
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,10 @@ | ||
package usecases.cucumber; | ||
|
||
import org.junit.platform.suite.api.IncludeEngines; | ||
import org.junit.platform.suite.api.SelectClasspathResource; | ||
import org.junit.platform.suite.api.Suite; | ||
|
||
@Suite | ||
@IncludeEngines("cucumber") | ||
@SelectClasspathResource("usecases/cucumber") | ||
public class RunCucumberTest {} |
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 |