1
我試圖單元測試與RestTemplate發送到API的請求,並從中獲取一些資源次年春天啓動代碼:的Mockito和RestTemplate與泛型類型
final URI targetUri = UriComponentsBuilder.fromUriString(baseUri)
.path("/myEntities").build().toUri();
final RequestEntity<Void> request = RequestEntity.get(targetUri).accept(HAL_JSON).build();
final Resources<MyEntity> resourceAccounts = restTemplate.exchange(request, new ResourcesType<MyEntity>() {
}).getBody();
在單元測試中,我嘲諷使用此的Mockito請求 - 響應:
final Resources<MyEntity> myEntities = new Resources<>(myEntityList, links);
final ResponseEntity<Object> response = new ResponseEntity<Object>(myEntities, HttpStatus.OK);
when(restTemplate.exchange(any(RequestEntity.class), any(ResourcesType.class))).thenReturn(response);
它工作正常,但我是因爲我沒有正確地使用仿製藥獲得Unchecked invocation exchanged
。
我只是想知道做這件事的正確和防彈的方法是什麼?我嘗試將ResponseEntity
類型轉換爲MyEntity
,但這確實會導致編譯異常(構造函數ResponseEntity<MyEntity> is undefined
)。