Gheorghe Mita
01/07/2025, 7:21 PMIndexOutOfBoundsException
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:
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:
@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`:
<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!Werner Blanck
01/07/2025, 11:05 PMGheorghe Mita
01/08/2025, 12:30 AM@ExtendWith(WireMockExtension.class)
and manually initialized the WireMockServer
in my @BeforeEach
method. This approach worked perfectly.Gheorghe Mita
01/08/2025, 12:39 AM@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)
.Iwan
01/09/2025, 7:34 PMIwan
01/09/2025, 7:35 PM