Skip to content

WireMock Spring Boot drastically simplifies testing HTTP clients in Spring Boot & Junit 5 based integration tests.

License

Notifications You must be signed in to change notification settings

wiremock/wiremock-spring-boot

Repository files navigation

WireMock Spring Boot

WireMock Spring Boot library drastically simplifies WireMock configuration in a Spring Boot and JUnit 5 application.

Highlights

  • Fully declarative WireMock setup
  • Support for multiple WireMockServer instances - one per HTTP client as recommended in the WireMock documentation
  • Automatically sets Spring environment properties
  • Does not pollute Spring application context with extra beans

It is originally forked from Wiremock Spring Boot.

How to install

Add the dependency to wiremock-spring-boot:

<dependency>
    <groupId>org.wiremock.spring</groupId>
    <artifactId>wiremock-spring-boot</artifactId>
    <version>X</version>
    <scope>test</scope>
</dependency>
testImplementation "org.wiremock.spring:wiremock-spring-boot:X"

Example

Enable it with the @EnableWireMock annotation, like:

@SpringBootTest
@EnableWireMock
class DefaultPropertiesTest {

  @Value("${wiremock.server.baseUrl}")
  private String wiremockUrl;

  @Value("${wiremock.server.port}")
  private String wiremockPort;

  @BeforeEach
  public void before() {
    WireMock.stubFor(get("/the_default_prop_mock").willReturn(aResponse().withStatus(202)));
  }

  @Test
  void test() {
    RestAssured.baseURI = this.wiremockUrl;
    RestAssured.when().get("/the_default_prop_mock").then().statusCode(202);
  }
}

There are more running examples in the repo.

Annotations

  • @EnableWireMock adds test context customizer and enables WireMockSpringExtension.
  • @ConfigureWireMock creates a WireMockServer.
  • @InjectWireMock injects WireMockServer instance to a test.

Properties

By default these will be provided:

  • wiremock.server.baseUrl - Base URL of WireMock server.
  • wiremock.server.port - HTTP port of WireMock server.

These can be changed with:

@EnableWireMock(
    @ConfigureWireMock(
        baseUrlProperties = { "customUrl", "sameCustomUrl" },
        portProperties = "customPort"))
class CustomPropertiesTest {

 @Value("${customUrl}")
 private String customUrl;

 @Value("${sameCustomUrl}")
 private String sameCustomUrl;

 @Value("${customPort}")
 private String customPort;

Customizing mappings directory

By default, each WireMockServer is configured to load WireMock root from:

  1. Classpath
    1. wiremock/{server-name}
    2. stubs/{server-name}
    3. mappings/{server-name}
    4. wiremock
    5. stubs
    6. mappings
  2. Directory
    1. {CWD}/wiremock/{server-name}
    2. {CWD}/stubs/{server-name}
    3. {CWD}/mappings/{server-name}
    4. {CWD}/wiremock
    5. {CWD}/stubs
    6. {CWD}/mappings

It can be changed:

@EnableWireMock({
  @ConfigureWireMock(
      name = "fs-client",
      filesUnderClasspath = "some/classpath/resource",
      filesUnderDirectory = "or/a/directory")
})

Registering WireMock extensions

WireMock extensions can be registered independently with each @ConfigureWireMock:

@EnableWireMock({
    @ConfigureWireMock(extensions = { ... })
})

Credits

About

WireMock Spring Boot drastically simplifies testing HTTP clients in Spring Boot & Junit 5 based integration tests.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •  

Languages