Skip to content

Commit

Permalink
Merge pull request #51 from wiremock/feature/issue-50-name
Browse files Browse the repository at this point in the history
feat: using 'wiremock' as default name on @InjectWireMock (refs #50)
  • Loading branch information
tomasbjerre authored Oct 10, 2024
2 parents 116437c + 9de59b5 commit d6caf57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
18 changes: 9 additions & 9 deletions src/main/java/org/wiremock/spring/InjectWireMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit d6caf57

Please sign in to comment.