这是我要嘲笑的呼吁:
ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.POST, entity, Response.class);
嘲笑以下:
@Test
public void getSpecalityInventoryItemsCallPositiveTest(){
RestTemplateBuilder restTemplateBuilder = Mockito.mock(RestTemplateBuilder.class);
RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
GetSpecalityInventoryItemsCaller getSpecalityInventoryItemsCaller = new GetSpecalityInventoryItemsCaller(restTemplateBuilder,id,pw);
Response inventoryItemsRespone = new Response();
inventoryItemsRespone.setStatusCode("0000");
BDDMockito.given(restTemplateBuilder.messageConverters(BDDMockito.any(ArrayList.class))).willReturn(restTemplateBuilder);
BDDMockito.given(restTemplateBuilder.additionalInterceptors(BDDMockito.any(ArrayList.class))).willReturn(restTemplateBuilder);
BDDMockito.given(restTemplateBuilder.build()).willReturn(restTemplate);
ResponseEntity<Response> responseEntity = new ResponseEntity<Response>(inventoryItemsRespone, HttpStatus.OK);
BDDMockito.given(restTemplate.exchange(url,HttpMethod.POST,HttpEntity.EMPTY,Response.class)).willReturn(responseEntity);
Response response = getSpecalityInventoryItemsCaller.getSpecalityInventoryItemsCaller(rxNumber);
Assertions.assertThat(response.getStatusCode()).isEqualTo("0000");
}
但是执行getSpecalityInventoryItemsCaller.getSpecalityInventoryItemsCaller(rxNumber)
,restTemplate.exchange(url, HttpMethod.POST, entity, Response.class);
呼叫内部返回null响应
我需要自己的响应响应
inventoryItemsRespone = new Response();
inventoryItemsRespone.setStatusCode("0000");
没有得到我自己的回应,请帮助我。
我的猜测是,您正在做的固执是在确切的参数上匹配的。问题在于,实际方法可能会调用的参数可能包括与您的固执相同但与实际对象相同的参数。
您也应该尝试使用匹配器进行rettemplate固定:
BDDMockito.given(restTemplate.exchange(any(),any(),any(),any())).willReturn(responseEntity);
尽管您可能希望比我在这里用作示例的任何()匹配器更精确,但是值得一看的是,看看这是否有效。