I have the next static mock:
try (MockedStatic<WebClient> webClientStatic = Mockito.mockStatic(WebClient.class)) {
webClientStatic.when(WebClient::builder).thenReturn(webClientBuilder);
webClientStatic.when(WebClient::builder).thenReturn(webClientBuilder);
when(webClientBuilder.clientConnector(Mockito.any(ReactorClientHttpConnector.class))).thenReturn(webClientBuilder);
when(webClientBuilder.baseUrl(anyString())).thenReturn(webClientBuilder);
when(webClientBuilder.build()).thenReturn(webClient);
when(webClient.post()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.header(anyString(), anyString())).thenReturn(requestBodySpec);
when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
when(requestBodySpec.retrieve()).thenReturn(responseSpec);
when(responseSpec.bodyToMono(ZoomAccessToken.class)).thenReturn(Mono.just(zoomAccessToken));
}
It worked when webClient.post()
or webClientBuilder.build()
is called one time, but I have a method in which webClient.post()
andwebClientBuilder.build()
are called a second time, the second time I am not getting a mocked object, but the real instance of webClient
and I am getting errors such as 401 Unauthorized from POST
do you know how can I fix it, or why the second time the static mock is not working? Thanks!!