Skip to content

Commit 1b42d76

Browse files
authoredJan 20, 2025
Merge pull request #77 from wiremock/feature/issue-73-register-spring-bean
feat: register WireMockServer as a Spring Bean (refs #73)
2 parents 53a4378 + 6379c85 commit 1b42d76

File tree

9 files changed

+140
-2
lines changed

9 files changed

+140
-2
lines changed
 

‎build.gradle

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,25 @@ dependencies {
3535
implementation platform("org.eclipse.jetty:jetty-bom:12.0.15")
3636
api "org.wiremock:wiremock-jetty12:${wiremockVersion}"
3737

38-
api "org.springframework.boot:spring-boot-test:3.3.4"
38+
api "org.springframework.boot:spring-boot-test:3.4.1"
3939
api "org.springframework:spring-test:6.1.13"
4040
api "org.slf4j:slf4j-api:2.0.16"
4141
api 'org.junit.jupiter:junit-jupiter-api:5.11.2'
4242

4343
testImplementation "org.wiremock:wiremock-jetty12:${wiremockVersion}"
44-
testImplementation "org.springframework.boot:spring-boot-starter-test:3.3.4"
44+
testImplementation "org.springframework.boot:spring-boot-starter-test:3.4.1"
4545
testImplementation 'org.assertj:assertj-core:3.26.3'
4646
testImplementation platform('org.junit:junit-bom:5.11.2')
4747
testImplementation 'org.junit.jupiter:junit-jupiter'
4848
testImplementation 'org.junit.platform:junit-platform-launcher'
4949
testImplementation 'io.rest-assured:rest-assured:5.5.0'
50+
testImplementation 'io.rest-assured:rest-assured:5.5.0'
51+
testImplementation "org.springframework.boot:spring-boot-starter-web:3.4.1"
52+
testImplementation 'io.cucumber:cucumber-java:7.20.1'
53+
testImplementation 'io.cucumber:cucumber-spring:7.20.1'
54+
testImplementation 'io.cucumber:cucumber-junit-platform-engine:7.20.1'
55+
testImplementation 'org.junit.platform:junit-platform-suite:1.11.4'
56+
testImplementation 'org.assertj:assertj-core:3.26.3'
5057

5158
constraints {
5259
implementation('org.apache.commons:commons-compress:1.26.0') {

‎src/main/java/org/wiremock/spring/ConfigureWireMock.java

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.github.tomakehurst.wiremock.extension.ExtensionFactory;
77
import java.lang.annotation.Retention;
88
import java.lang.annotation.RetentionPolicy;
9+
import org.springframework.beans.factory.annotation.Autowired;
910

1011
/**
1112
* Configures WireMock instance.
@@ -125,4 +126,10 @@
125126
* between test runs.
126127
*/
127128
boolean resetWireMockServer() default true;
129+
130+
/**
131+
* If <code>true</code>, it will register {@link WireMockServer} as a Spring Bean so that it can
132+
* be {@link Autowired} by name.
133+
*/
134+
boolean registerSpringBean() default false;
128135
}

‎src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java

+5
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ public WireMockServer createWireMockServer(
162162
});
163163
}
164164

165+
if (options.registerSpringBean()) {
166+
this.logger.info("Registering WireMockServer '" + options.name() + "' as a Spring Bean.");
167+
context.getBeanFactory().registerSingleton(options.name(), newServer);
168+
}
169+
165170
return newServer;
166171
}
167172

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package usecases;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
6+
import com.github.tomakehurst.wiremock.WireMockServer;
7+
import io.restassured.RestAssured;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Qualifier;
11+
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.wiremock.spring.ConfigureWireMock;
14+
import org.wiremock.spring.EnableWireMock;
15+
16+
@SpringBootTest
17+
@EnableWireMock(@ConfigureWireMock(name = "mywiremock", registerSpringBean = true))
18+
class AutowireNamedWireMockServerTest {
19+
20+
@Qualifier("mywiremock")
21+
@Autowired
22+
private WireMockServer wireMockServer;
23+
24+
@Value("${wiremock.server.baseUrl}")
25+
private String wiremockUrl;
26+
27+
@Test
28+
void returnsTodos() {
29+
this.wireMockServer.stubFor(get("/ping").willReturn(aResponse().withStatus(200)));
30+
31+
RestAssured.when().get(this.wiremockUrl + "/ping").then().statusCode(200);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package usecases.cucumber;
2+
3+
public class CucumberConstants {
4+
public static final String WIREMOCK_SERVER_NAME = "my-wiremock-server";
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package usecases.cucumber;
2+
3+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
4+
5+
import io.cucumber.spring.CucumberContextConfiguration;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.wiremock.spring.ConfigureWireMock;
8+
import org.wiremock.spring.EnableWireMock;
9+
10+
@CucumberContextConfiguration
11+
@SpringBootTest(webEnvironment = RANDOM_PORT)
12+
@EnableWireMock(
13+
@ConfigureWireMock(name = CucumberConstants.WIREMOCK_SERVER_NAME, registerSpringBean = true))
14+
public class CucumberSpringConfiguration {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package usecases.cucumber;
2+
3+
import org.junit.platform.suite.api.IncludeEngines;
4+
import org.junit.platform.suite.api.SelectClasspathResource;
5+
import org.junit.platform.suite.api.Suite;
6+
7+
@Suite
8+
@IncludeEngines("cucumber")
9+
@SelectClasspathResource("usecases/cucumber")
10+
public class RunCucumberTest {}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Feature: Test that features can be used
2+
3+
Scenario: Setup WireMock in Given and try it out in then
4+
Given WireMock has endpint ping
5+
When WireMock is invoked with ping
6+
Then it should respond 200
7+
When WireMock is invoked with pang
8+
Then it should respond 404

0 commit comments

Comments
 (0)