亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

模擬用于單元測試的請求實體調用

模擬用于單元測試的請求實體調用

素胚勾勒不出你 2022-09-01 18:13:59
我想模擬請求實體和響應以測試控制器方法上的方法,此代碼已由另一個開發人員編寫,我應該使用mockito對其進行測試。我試圖模擬請求實體值和respionse實體值,但它不起作用,當我嘗試調試時,我得到一個反射錯誤    public class InquiryController {private static final Logger log =     LoggerFactory.getLogger(InquiryController.class);@Autowiredprivate InquiryProperties inquiryProperties;@Autowiredprivate InquiryService inquiryService;@AutowiredRestTemplate restTemplate;public static int count = 0;@Beanprivate 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);}
查看完整描述

2 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

這是因為在執行 REST 調用時,實例未通過 注入。您需要在組件類中聲明 的方法,該方法在應用程序啟動期間或換句話說,在組件掃描期間進行掃描。從而可用于 .RestTemplateSpring IOCgetRestTemplaterestTemplateautowire



查看完整回答
反對 回復 2022-09-01
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

按照@chrylis建議將配置與控制器分離后,您可以像這樣繼續操作。


您必須嘗試模擬 RequestEntity.post 方法。請注意,它是一個靜態方法,其模擬方式與通常的公共實例方法略有不同。為此,您需要使用PowerMockito,因為Mockito不會這樣做。


在 pom 中添加依賴項,如下所示:


<dependency>

    <groupId>org.powermock</groupId>

    <artifactId>powermock-module-junit4</artifactId>

    <version>1.6.5</version>

    <scope>test</scope>

</dependency>

<dependency>

    <groupId>org.powermock</groupId>

    <artifactId>powermock-api-mockito</artifactId>

    <version>1.6.5</version>

    <scope>test</scope>

</dependency>

然后用 注釋測試類,如下所示:@RunWith@PrepareForTest


@RunWith(PowerMockRunner.class)

@PrepareForTest({RequestEntity.class})

public class TestClass {

}

和模擬 post 方法,如下所示:


PowerMockito.mockStatic(RequestEntity.class);  when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());

private RequestEntity< CustomerInfo > getRequestEntityResponseBody(){

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


查看完整回答
反對 回復 2022-09-01
  • 2 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號