首页 > 解决方案 > 创建虚拟请求实体对象以在模拟测试请求 .post 方法时返回

问题描述

我正在尝试模拟一个请求实体对象来测试RequestEntity.post方法。

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

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

我想使用 mockito 来模拟这个方法,并且在“when”中我想为此返回一个虚拟对象。

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

            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);

}

标签: javajunitmockitopowermockito

解决方案


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

@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));
    }
}

推荐阅读