这是我的嘲笑:
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);
内部调用返回空响应
我需要自己的回复回复
inventoryItemsRespone = new Response();
inventoryItemsRespone.setStatusCode("0000");
没有得到我自己的答复,请帮帮我。
答案 0 :(得分:0)
我的猜测是你正在做的存根是在精确的参数上匹配。问题是实际的方法可能调用可能包含与你的存根具有相同值但与实际对象不同的参数。
你也应该尝试使用matchers来进行restTemplate存根:
BDDMockito.given(restTemplate.exchange(any(),any(),any(),any())).willReturn(responseEntity);
虽然您可能希望使其比我在此处使用的any()匹配器更精确,但是值得测试以确定它是否有效。