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

问题描述 投票:0回答:1

我试图模拟一个请求实体objcet来测试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);

}
java junit mockito powermockito
1个回答
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));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.