Wiremock in Spring Boot


1. Add dependencies

In your pom.xml (for Maven) include:

<dependencies>
    <!-- Spring Boot Starter Test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- WireMock for mocking HTTP services -->
    <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock-jre8</artifactId>
        <version>2.35.0</version>
        <scope>test</scope>
    </dependency>

    <!-- Optional: RestTemplate if using it for HTTP calls -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2. Example Service

Suppose you have a service that calls an external API:

@Service
public class UserService {

    private final RestTemplate restTemplate;

    public UserService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getUserNameById(Long id) {
        String url = "http://localhost:8089/users/" + id;
        return restTemplate.getForObject(url, String.class);
    }
}

3. Test with WireMock

Create a test class for UserService:

import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.client.RestTemplate;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
public class UserServiceTest {

    @RegisterExtension
    static WireMockExtension wireMockRule = WireMockExtension.newInstance()
            .options(WireMock.configureFor(8089))
            .build();

    private RestTemplate restTemplate = new RestTemplateBuilder().build();
    private UserService userService = new UserService(restTemplate);

    @Test
    void testGetUserNameById() {
        // Stub the WireMock server
        wireMockRule.stubFor(get(urlEqualTo("/users/1"))
                .willReturn(aResponse()
                        .withHeader("Content-Type", "application/json")
                        .withBody("\"John Doe\"")));

        // Call the service
        String name = userService.getUserNameById(1L);

        // Verify the response
        assertEquals("John Doe", name);

        // Verify that WireMock received the request
        wireMockRule.verify(getRequestedFor(urlEqualTo("/users/1")));
    }
}

4. Key Points

  1. WireMockExtension runs a mock HTTP server.
  2. stubFor defines a mock response for a specific endpoint.
  3. You can verify that a request was made with verify.
  4. Port 8089 in the example is arbitrary; it must match your service’s test URL.

Leave a Reply