我如何使用Mockito为jsonparser,jsonmapping和io异常编写测试用例

时间:2019-03-01 06:06:27

标签: java unit-testing mockito

我想在控制器中模拟此服务,以便我可以对其进行测试。我如何模拟coreservice.getRelatedInfo抛出给定的异常进行测试。 我试图扔掉它们,但是,我遇到了错误。当我测试运行时异常时,它正在工作。

@PostMapping("/realtedcustomerinfo")
public ResponseEntity<List<CustomerInfoDTO>> 
getRelatedCustomerInfo(@RequestBody RelatedCustomerInfo 
relatedCustomerInfo) {
    log.info("Start of getRelatedCustomerInfo() input {}", 
 relatedCustomerInfo);
    List<CustomerInfoDTO> customerInfoDTOs = null;

    try {
        customerInfoDTOs = 
 coreService.getRelatedCustomerInfo(relatedCustomerInfo);
    } catch (JsonParseException e) {
        log.error("Exception details", e);
    } catch (JsonMappingException e) {
        log.error("Exception details", e);
    } catch (IOException e) {
        log.error("Exception details", e);
    }

    if (null == customerInfoDTOs) {
        log.info("End of getRelatedCustomerInfo() return {}", customerInfoDTOs);
        return new ResponseEntity<List<CustomerInfoDTO>>(new ArrayList<CustomerInfoDTO>(), HttpStatus.OK);
    }

    log.info("End of getRelatedCustomerInfo() return {}", customerInfoDTOs);
    return new ResponseEntity<List<CustomerInfoDTO>>(customerInfoDTOs, HttpStatus.OK);
}

0 个答案:

没有答案