I am having an issue when trying to mock restTemplateBuilder:
private RestTemplate restTemplate() {
HttpClient client = HttpClients.custom().build();
return restTemplateBuilder.
requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client)).
build();
}
My test method setup below:
@Before
public void setUp() {
when(restTemplateBuilder.requestFactory(() -> any(ClientHttpRequestFactory.class))).thenReturn(restTemplateBuilder);
when(restTemplateBuilder.build()).thenReturn(restTemplate);
}
In this case, requestFactory always returns null. Mockito also gives me a hint that first line in setup is not in use, and ask on the line with requestFactory if "args ok?".