user
user

Reputation: 914

How do I mock HTTPSClient post service

I want to mock the following line of code:

ResponseEntity<String> response = client.callPostService(url, dto, new ParameterizedTypeReference<String>(){});

Attempt

@Test
public void testFunction{
    HTTPSClient client = Mockito.mock(HTTPSClient.class);
    Mockito.when(client.callPostService(any(String.class),any(Dto.class), new ParameterizedTypeReference<String>{}))
}

I get errors regarding the parameters I have placed.

Upvotes: 0

Views: 64

Answers (1)

amseager
amseager

Reputation: 6391

You shouldn't mix Mockito's argument matchers (like any(), eq() etc.) and the real objects while configuring behavior for a mock.

So, in your case the next would be correct:

Mockito.when(client.callPostService(any(String.class),any(Dto.class), Mockito.any(ParameterizedTypeReference.class))).thenReturn(...)

or (since Java 8):

Mockito.when(client.callPostService(any(String.class),any(Dto.class), Mockito.any())).thenReturn(...)

The latter also doesn't raise the compiler warning about the unchecked cast of generic type because of enhanced type inference.

Upvotes: 1

Related Questions