How to mock Spring Webclient

1.1k Views Asked by At

I am trying to write a unit test case using mockito but getting null pointer exception when calling retrieve. Can someone help me how to fix this?

Method

public Mono<String> retrieveSites() {
return restAPIClient.get().uri(builder -> builder.path("/someurl")
     .queryParam("param1","value1")
     .queryParam(param2,"value2")
     .build())
**.retrieve()**
.onStatus(HttpStatus::is4xxClientError, this::processClientError)
.onStatus((HttpStatus::isError), this::processServerError).bodyToMono(String.class);
}

TestCase

  @Test
    public void shouldReturnRetrieveSites() {
        when(webClient.get()).thenReturn(requestHeadersUriSpec);
        when(builder.path(anyString())).thenReturn(builder);
        when(builder.queryParam(anyString(), anyString())).thenReturn(builder);
        when(builder.path(anyString())).thenReturn(builder);
        when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
        when(responseSpec.bodyToMono(ArgumentMatchers.<Class<String>>notNull())).thenReturn(Mono.empty());
        assertNotNull(webClientService.retrieveSites());
    }
0

There are 0 best solutions below