2015-10-14 23 views
0

我有一個類似於mRestAccess.exchange(url, HttpMethod.GET, request, byte[].class);的代碼現在我想用JUnit來測試它。匹配參數String.class,byte []。class Junit

當我寫Mockito.doReturn(stringResponse).when(mRestAccess).exchange(Mockito.anyString(), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class),???????);

什麼代替寫????????

回答

-1

使用下面的語句來模擬使用powermockito的spring restTemplate。

PowerMockito.when(restTemplate.exchange(
    Matchers.anyString(), 
    Matchers.any(HttpMethod.class), 
    Matchers.<HttpEntity<?>> any(), 
    Matchers.any(Class.class))) 
.thenReturn(respEntity); 

請參閱此link的詳細說明。

+0

請解釋爲什麼這裏需要Powermockito,當它不是在問題的標籤,並開發了答案現場進一步,而不是將一個鏈接後面的解釋到自己的網站。 –

0

您可以使用Mockito.eq來測試參數相等(使用equals)。爲了獲得更大的靈活性,any(Class.class)可以工作,但可能不會限制類型參數T足以適用於某些Mockito語法。

Mockito.doReturn(stringResponse) 
    .when(mRestAccess) 
    .exchange(
     Mockito.anyString(), 
     Mockito.any(HttpMethod.class), 
     Mockito.any(HttpEntity.class), 
     Mockito.eq(byte[].class));