I'm trying to mock the WebClient
in one of my tests. I found some examples online where people do the same.
One example from spring-data-elasticsearch and another one from some tutorials.
Here is my own example.
@Test
public void mytest() {
WebClient webClient = mock(WebClient.class);
RequestHeadersUriSpec headersUriSpec = mock(RequestHeadersUriSpec.class);
when(webClient.get()).thenReturn(headersUriSpec);
}
Unfortunately I see some warnings. Here is what I got:
WebClient.RequestHeadersUriSpec is a raw type. References to generic type WebClient.RequestHeadersUriSpec should be parameterized
When I change my code and add a wildcard to the RequestHeadersUriSpec
I get another error message.
@Test
public void mytest() {
WebClient webClient = mock(WebClient.class);
RequestHeadersUriSpec<?> headersUriSpec = mock(RequestHeadersUriSpec.class);
when(webClient.get()).thenReturn(headersUriSpec);
}
The method thenReturn(WebClient.RequestHeadersUriSpec<capture#1-of ?>) in the type OngoingStubbing<WebClient.RequestHeadersUriSpec<capture#1-of ?>> is not applicable for the arguments (WebClient.RequestHeadersUriSpec<capture#3-of ?>)
If I let Java infer the type I'm getting a third message.
@Test
public void mytest() {
WebClient webClient = mock(WebClient.class);
var headersUriSpec = mock(RequestHeadersUriSpec.class);
when(webClient.get()).thenReturn(headersUriSpec);
}
Type safety: The expression of type WebClient.RequestHeadersUriSpec needs unchecked conversion to conform to WebClient.RequestHeadersUriSpec<capture#1-of ?>
Now I'm wondering
- Why does it work for the other projects?
- How can I solve my problem?
Thank you very much!
Best regards, Mirco