Subject: `IndexOutOfBoundsException` in WireMock d...
# help
g
Subject:
IndexOutOfBoundsException
in WireMock during Test Setup Message: Hi WireMock team, I’m encountering an issue when running tests with WireMock in my Java project. The error occurs during the initialization of the
WireMockExtension
in my test class:
Copy code
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
    at java.base/java.util.Objects.checkIndex(Objects.java:385)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at com.github.tomakehurst.wiremock.http.HttpServerFactoryLoader.pickMostAppropriateFrom(HttpServerFactoryLoader.java:77)
    at com.github.tomakehurst.wiremock.http.HttpServerFactoryLoader.load(HttpServerFactoryLoader.java:57)
    at com.github.tomakehurst.wiremock.WireMockServer.getHttpServerFactory(WireMockServer.java:89)
    at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:72)
    at com.github.tomakehurst.wiremock.junit5.WireMockExtension.startServerIfRequired(WireMockExtension.java:215)
    at com.github.tomakehurst.wiremock.junit5.WireMockExtension.beforeAll(WireMockExtension.java:282)
Here’s the relevant part of my test setup:
Copy code
@ExtendWith(WireMockExtension.class)
@SpringBootTest
public class WeatherControllerTest {

    private final RestTemplate restTemplate = new RestTemplate();

    @Test
    public void testAddWeather() {
        stubFor(post(urlEqualTo("/api/weather"))
            .willReturn(aResponse()
                .withStatus(201)
                .withBody("Weather data added successfully.")));

        String url = "<http://localhost:8080/api/weather>";
        String payload = "{ \"location\": { \"cityName\": \"New York\" }, \"condition\": { \"temperature\": 22.5 }, \"timestamp\": \"2025-01-01T10:00:00\" }";

        ResponseEntity<String> response = restTemplate.postForEntity(url, payload, String.class);

        assertEquals(HttpStatus.CREATED, response.getStatusCode());
        assertEquals("Weather data added successfully.", response.getBody());

        verify(postRequestedFor(urlEqualTo("/api/weather")));
    }
}
Steps I’ve Tried: 1. Ensured that WireMock is included in my `pom.xml`:
Copy code
<dependency>
            <groupId>org.wiremock</groupId>
            <artifactId>wiremock</artifactId>
            <version>3.10.0</version>
            <scope>test</scope>
        </dependency>
2. Cleaned and rebuilt the project. Has anyone encountered this issue before? Is there a missing dependency or configuration I need to include? Any help would be appreciated. Thanks in advance!
1
w
I had a similar issue, I think it was because of a conflict on the jetty versions. I fixed it by using the spring-boot integration: https://wiremock.org/docs/spring-boot/ https://github.com/wiremock/wiremock-spring-boot
g
Thanks. I got rid of
@ExtendWith(WireMockExtension.class)
and manually initialized the
WireMockServer
in my
@BeforeEach
method. This approach worked perfectly.
👍 1
I thought I could use
@ExtendWith(WireMockExtension.class)
to reduce the boilerplate but for some reason it is not working. I still somewhat new to this and I do not know how to really troubleshoot it using
@ExtendWith(WireMockExtension.class)
.
i
When you use the Spring Boot integration, you can use @EnableWireMock at the class level. It is quite neat and works nicely. In case you can't do that, you can still use the Spring Boot integration but with an explicit WireMock instantiation. I wrote a tutorial: https://medium.com/@arc-e-tect/wiremock-cucumber-and-springboot-ae3e107bd3d7
The tutorial shows how to use WireMock with a Cucumber Runner, but you can use the same approach in a plain Spring Boot application
133 Views