创建虚拟请求实体对象以在模拟测试请求.post方法时返回

时间:2019-03-25 11:39:48

标签: java junit mockito powermockito

我正在尝试模拟用于测试 RequestEntity.post 方法的请求实体objcet。

   RequestEntity<CustomerInfo> body = RequestEntity.post(new 
    URI(inquiryProperties.getEndCustomer()))

  .accept(MediaType.APPLICATION_JSON).body(customerInfo);

我想使用模拟方法模拟此方法,并且在“当”时要为此返回一个虚拟对象。

这是我尝试为控制器模拟的方法。

            private static final Logger log = LoggerFactory.getLogger(InquiryController.class);

@Autowired
private InquiryProperties inquiryProperties;

@Autowired
private InquiryService inquiryService;


@Autowired
RestTemplate restTemplate;

public static int count = 0;


@Bean
private RestTemplate getRestTemplate() {
    return new RestTemplate();
}





        public ResponseEntity<List<EndCustomerDTO>> endCustomer(@RequestBody CustomerInfo customerInfo)
        throws IOException, JSONException {

    log.info("### InquiryController.endCustomer() ===>");
    List<EndCustomerDTO> endCustomerDTOs = null;

    try {

        //RestTemplate restTemplate = new RestTemplate();
        RequestEntity<CustomerInfo> body = RequestEntity.post(new URI(inquiryProperties.getEndCustomer()))
                .accept(MediaType.APPLICATION_JSON).body(customerInfo);
        ResponseEntity<List<EndCustomerDTO>> response = restTemplate.exchange(body,
                new ParameterizedTypeReference<List<EndCustomerDTO>>() {
                });
        endCustomerDTOs = (response != null ? response.getBody() : new ArrayList<EndCustomerDTO>());

    } catch (RestClientException | URISyntaxException e) {
        log.error("InquiryController.endCustomer()" + e.getMessage());
    }

    log.info("### END InquiryController.endCustomer()  ===>");

    if (null == endCustomerDTOs) {
        return new ResponseEntity<List<EndCustomerDTO>>(new ArrayList<EndCustomerDTO>(), HttpStatus.OK);
    }
    return new ResponseEntity<List<EndCustomerDTO>>(endCustomerDTOs, HttpStatus.OK);

}

1 个答案:

答案 0 :(得分:0)

带有Mockito的示例,用于模拟REST模板交换和监视响应实体

@RunWith(MockitoJUnitRunner.class)
public class CustomerTest {

    @Mock
    RestTemplate restTemplate;
    @Spy
    ResponseEntity responseEntity = mock(ResponseEntity.class);
    @Inject
    InquiryService inquiryService;

    @Test
    public void endCustomerTest() {
    EndCustomerDTO yourEndCustomerDTO= new EndCustomerDTO();
    //define the entity you want the exchange to return
    ResponseEntity<List<EndCustomerDTO >> yourEndCustomerDTOEntity = new ResponseEntity<List<EndCustomerDTO >>(HttpStatus.ACCEPTED);
    Mockito.when(restTemplate.exchange(
        Matchers.eq("/urlPattern/urlPath"),
        Matchers.eq(HttpMethod.POST),
        Matchers.<HttpEntity<List<EndCustomerDTO >>>any(),
        Matchers.<ParameterizedTypeReference<List<EndCustomerDTO >>>any())
    ).thenReturn(yourEndCustomerDTOEntity);
    //service call to get the end customers
    List<EndCustomerDTO > result = inquiryService.getEndCustomers();
    //asserting by comparing expected and actual value
    Assert.assertEquals(yourEndCustomerDTOEntity , result .get(0));
    }
}