I am new to Spring Webflux and I want to know what is the best way to write a unit test to check if an error is being thrown:
public Mono<String> update(UpdateModel updateModel) {
return webClient.post()
.uri(updatePath)
.bodyValue(updateModel)
.retrieve()
.onStatus(httpStatus -> !HttpStatus.OK.equals(httpStatus),
clientResponse -> Mono.error(new ServerSideException("Failed to update")))
.bodyToMono(String.class);
}
What is the best way to write a unit test ?
This is what I am using:
@Test
public void shouldThrowServerErrorIfUpdateServiceCallFails() {
when(responseSpecMock.onStatus(any(), any())).thenReturn(responseSpecMock);
when(responseSpecMock.bodyToMono(String.class)).thenReturn(Mono.error(new ServerSideException("Failed to update")));
var response = client.update(updateModel);
StepVerifier.create(response)
.expectError(ServerSideException.class)
.verify();
}
The error is actually thrown from the lambda, not being thrown from bodyToMono call.
This is obviously not correct, but I don't know any better way to check it via unit test.