Reputation: 16395
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
Upvotes: 2
Views: 4676
Reputation: 376
Could you try with requestHeadersUriSpecMock and webClient defined like this:
@Mock
private WebClient webClientMock;
@SuppressWarnings("rawtypes")
@Mock
private WebClient.RequestHeadersUriSpec requestHeadersUriSpecMock;
and then call, more or less as you wanted: when(webClientMock.get()).thenReturn(requestHeadersUriSpecMock);
Upvotes: 1