模拟用于单元测试的请求实体调用

我想模拟请求实体和响应以测试控制器方法上的方法,此代码已由另一个开发人员编写,我应该使用mockito对其进行测试。


我试图模拟请求实体值和respionse实体值,但它不起作用,当我尝试调试时,我得到一个反射错误


    public class InquiryController {


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

}


    @PostMapping(value = "/endCustomer", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {

        MediaType.APPLICATION_JSON_VALUE })

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


}


素胚勾勒不出你
浏览 74回答 2
2回答

桃花长相依

这是因为在执行 REST 调用时,实例未通过 注入。您需要在组件类中声明 的方法,该方法在应用程序启动期间或换句话说,在组件扫描期间进行扫描。从而可用于 .RestTemplateSpring IOCgetRestTemplaterestTemplateautowire

宝慕林4294392

按照@chrylis建议将配置与控制器分离后,您可以像这样继续操作。您必须尝试模拟 RequestEntity.post 方法。请注意,它是一个静态方法,其模拟方式与通常的公共实例方法略有不同。为此,您需要使用PowerMockito,因为Mockito不会这样做。在 pom 中添加依赖项,如下所示:<dependency>&nbsp; &nbsp; <groupId>org.powermock</groupId>&nbsp; &nbsp; <artifactId>powermock-module-junit4</artifactId>&nbsp; &nbsp; <version>1.6.5</version>&nbsp; &nbsp; <scope>test</scope></dependency><dependency>&nbsp; &nbsp; <groupId>org.powermock</groupId>&nbsp; &nbsp; <artifactId>powermock-api-mockito</artifactId>&nbsp; &nbsp; <version>1.6.5</version>&nbsp; &nbsp; <scope>test</scope></dependency>然后用 注释测试类,如下所示:@RunWith@PrepareForTest@RunWith(PowerMockRunner.class)@PrepareForTest({RequestEntity.class})public class TestClass {}和模拟 post 方法,如下所示:PowerMockito.mockStatic(RequestEntity.class);&nbsp; when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());private RequestEntity< CustomerInfo > getRequestEntityResponseBody(){&nbsp;//code}更新CustomerInfo customerInfo = new CustomerInfo();HttpHeaders responseHeaders = new HttpHeaders();responseHeaders.set("MyResponseHeader", "MyValue");RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);PowerMockito.mockStatic(RequestEntity.class);when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java