2014-10-08 102 views
0

我們使用的是Gmail API Java客戶端版本1.19.0。是否有任何人已經成功地實施了可用於stubing請求,如工作mock對象:Gmail Api Java客戶端 - 使用mockito/powermock示例來模擬Gmail API調用

gmailClient.users().history().list("me").setStartHistoryId(startHistoryId).setPageToken(pageToken).execute(); 

從本質上講,我們想存根上面的調用,並創建一個特定的響應,以測試不同的業務場景。

回答

1

請檢查下面的問題的工作示例。不需要使用powermock。 Mockito只是需要的。

@Before 
    public void init() throws Exception{ 
     ListHistoryResponse historyResponse = new ListHistoryResponse(); 
     historyResponse.setHistoryId(BigInteger.valueOf(1234L)); 
     List<History> historyList = new ArrayList<>(); 
     History historyEntry = new History(); 
     Message message = new Message(); 
     message.setId("123456"); 
     message.setThreadId("123456"); 
     List<Message> messages = new ArrayList<>(); 
     messages.add(message); 
     historyEntry.setMessages(messages); 
     historyList.add(historyEntry); 

     mock = mock(Gmail.class); 
     Gmail.Users users = mock(Gmail.Users.class); 
     Gmail.Users.History history = mock(Gmail.Users.History.class); 
     Gmail.Users.History.List list = mock(Gmail.Users.History.List.class); 
     when(mock.users()).thenReturn(users); 
     when(users.history()).thenReturn(history); 
     when(history.list("me")).thenReturn(list); 
     when(list.setStartHistoryId(BigInteger.valueOf(123L))).thenReturn(list); 
     when(list.setPageToken(null)).thenReturn(list); 
     when(list.execute()).thenReturn(historyResponse); 

} 
0

你可以嘲笑這些課程,因爲它們不是最終的,等等。這裏有什麼限制? (沒有看過Google Java客戶端庫的源代碼,但不應該是特定於gmail的 - 如果你發現某人爲另一個Google Java客戶端API做了它,你應該可以重新使用它)。