模拟 RestTemplate return 空

Mock RestTemplate return null

我需要为一个简单的请求模拟 restTemplate:

HttpEntity<RequestVO> request = new HttpEntity<>(new RequestVO(),new HttpHeaders());
restTemplate.postForEntity("URL", request , ResponseVO.class);

但我在 postForEntity 请求上得到空值:

ResponseVO respVO = new ResponseVO();
respVO.setEntry("https://www.test.com");
ResponseEntity<Object> resp =new ResponseEntity<>(
    respVO,
    HttpStatus.OK
);      
when(restTemplate.postForEntity(any(), any(), any())).thenReturn(resp);

试图关注,我正在嘲笑相关对象:

@Mock
HttpHeaders httpHeaders;
@Mock
ResponseEntity responseEntity;
@Mock   
private RestTemplate restTemplate;

EDIT 与@JoãoDias 建议的类似尝试相同 null 结果

when(restTemplate.postForEntity(anyString(), any(HttpEntity.class), eq(ResponseVO.class))).thenReturn(resp);

成功 MockRestServiceServer:

@Autowired
private RestTemplate restTemplate;

private MockRestServiceServer mockServer;
@BeforeClass
public void initMocks() {       
    mockServer = MockRestServiceServer.createServer(restTemplate);
}
...
mockServer.expect(ExpectedCount.once(), 
              requestTo(new URI("URL")))
              .andExpect(method(HttpMethod.POST))
              .andRespond(withStatus(HttpStatus.OK)
              .contentType(MediaType.APPLICATION_JSON)
              .body("EXPECTED")

MockRestServiceServer actually works by intercepting the HTTP API calls using a MockClientHttpRequestFactory. Based on our configuration, it creates a list of expected requests and corresponding responses. When the RestTemplate instance calls the API, it looks up the request in its list of expectations, and returns the corresponding response.