I am in the process of writing unit tests for a reactive API. I am using spring-boot 2.7.8 and mockwebserver 4.10.0. The method under test simply calls a third-party API with WebClient. I am having trouble writing the test for an error case. The purpose of the test is to make sure that the retry strategy is working properly.
When running this test, it hangs indefinitely, it does not succeed or fail. I noticed that this behavior does not occur if I remove the retryWhen statement.
Here is a simplified version of the method under test:
public Mono<Employee> getEmployeeByEmpNumber(String employeeNumber) {
return empWebClnt.get()
.uri(uriBuilder -> uriBuilder.queryParam("employeeNumber", employeeNumber)
.build())
.retrieve().bodyToFlux(Employee.class).next()
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2)).jitter(0.5))
.onErrorResume(e -> {
return Mono.error(new EmployeeSearchException(e));
});
}
And here is the test method:
@Test
void getEmployeeByEmpNumber_shouldRetryThreeTimesOnError()
throws InterruptedException {
mockWebServer.enqueue(new MockResponse().setResponseCode(500)
.addHeader("Content-Type", "application/json").setBody("{'result': 'error'}"));
mockWebServer.enqueue(new MockResponse().setResponseCode(500)
.addHeader("Content-Type", "application/json").setBody("{'result': 'error'}"));
mockWebServer.enqueue(new MockResponse().setResponseCode(500)
.addHeader("Content-Type", "application/json").setBody("{'result': 'error'}"));
StepVerifier.create(
employeeClient.getEmployeebyEmpNumber("HFH569"))
.expectError().verify();
mockWebServer.takeRequest();
assertEquals(3, mockWebServer.getRequestCount());
}
Any help on this would be greatly appreciated.