From 9de59b53e0c8d057b27127b941ad3f38e1a6de8a Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Thu, 10 Oct 2024 17:01:42 +0200 Subject: [PATCH] feat: using 'wiremock' as default name on @InjectWireMock (refs #50) --- .../org/wiremock/spring/InjectWireMock.java | 18 +++++----- ...st.java => InjectDefaultWireMockTest.java} | 26 ++++++++------- .../java/app/InjectNamedWireMockTest.java | 33 +++++++++++++++++++ 3 files changed, 56 insertions(+), 21 deletions(-) rename wiremock-spring-boot-example/src/test/java/app/{InjectWireMockTest.java => InjectDefaultWireMockTest.java} (61%) create mode 100644 wiremock-spring-boot-example/src/test/java/app/InjectNamedWireMockTest.java diff --git a/src/main/java/org/wiremock/spring/InjectWireMock.java b/src/main/java/org/wiremock/spring/InjectWireMock.java index 9fa1ab3..40816b2 100644 --- a/src/main/java/org/wiremock/spring/InjectWireMock.java +++ b/src/main/java/org/wiremock/spring/InjectWireMock.java @@ -6,19 +6,19 @@ import java.lang.annotation.Target; /** - * Injects WireMock instance previously configured on the class or field level with {@link - * ConfigureWireMock}. + * Injects WireMock instance previously configured on the class or field level + * with {@link ConfigureWireMock}. * * @author Maciej Walkowiak */ -@Target({ElementType.FIELD, ElementType.PARAMETER}) +@Target({ ElementType.FIELD, ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) public @interface InjectWireMock { - /** - * The name of WireMock instance to inject. - * - * @return the name of WireMock instance to inject. - */ - String value(); + /** + * The name of WireMock instance to inject. + * + * @return the name of WireMock instance to inject. + */ + String value() default "wiremock"; } diff --git a/wiremock-spring-boot-example/src/test/java/app/InjectWireMockTest.java b/wiremock-spring-boot-example/src/test/java/app/InjectDefaultWireMockTest.java similarity index 61% rename from wiremock-spring-boot-example/src/test/java/app/InjectWireMockTest.java rename to wiremock-spring-boot-example/src/test/java/app/InjectDefaultWireMockTest.java index f6b2fd5..9781f23 100644 --- a/wiremock-spring-boot-example/src/test/java/app/InjectWireMockTest.java +++ b/wiremock-spring-boot-example/src/test/java/app/InjectDefaultWireMockTest.java @@ -3,28 +3,30 @@ 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.Value; import org.springframework.boot.test.context.SpringBootTest; import org.wiremock.spring.EnableWireMock; import org.wiremock.spring.InjectWireMock; +import com.github.tomakehurst.wiremock.WireMockServer; + +import io.restassured.RestAssured; + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @EnableWireMock -class InjectWireMockTest { +class InjectNamedWireMockTest { - @InjectWireMock("wiremock") - private WireMockServer wireMockServer; + @InjectWireMock + private WireMockServer wireMockServer; - @Value("${wiremock.server.baseUrl}") - private String wiremockUrl; + @Value("${wiremock.server.baseUrl}") + private String wiremockUrl; - @Test - void returnsTodos() { - this.wireMockServer.stubFor(get("/ping").willReturn(aResponse().withStatus(200))); + @Test + void returnsTodos() { + this.wireMockServer.stubFor(get("/ping").willReturn(aResponse().withStatus(200))); - RestAssured.when().get(this.wiremockUrl + "/ping").then().statusCode(200); - } + RestAssured.when().get(this.wiremockUrl + "/ping").then().statusCode(200); + } } diff --git a/wiremock-spring-boot-example/src/test/java/app/InjectNamedWireMockTest.java b/wiremock-spring-boot-example/src/test/java/app/InjectNamedWireMockTest.java new file mode 100644 index 0000000..2b1fd82 --- /dev/null +++ b/wiremock-spring-boot-example/src/test/java/app/InjectNamedWireMockTest.java @@ -0,0 +1,33 @@ +package app; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.wiremock.spring.ConfigureWireMock; +import org.wiremock.spring.EnableWireMock; +import org.wiremock.spring.InjectWireMock; + +import com.github.tomakehurst.wiremock.WireMockServer; + +import io.restassured.RestAssured; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@EnableWireMock(@ConfigureWireMock(name = "mywiremock")) +class InjectDefaultWireMockTest { + + @InjectWireMock("mywiremock") + 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); + } +}