2016-05-12 26 views
1

我得到一個空指針異常。當我嘲笑一個鏈式方法調用時,Powermock鏈式方法調用在最後一類

我的代碼看起來像這樣:

@RunWith(PowerMockRunner.class) 
@PrepareForTest({Comment.class, CommentThread.class}) 
public class YoutubeTest { 

@Test 
public void testSortCommentsByDate() { 
Comment youtubeCommentOne = PowerMockito.mock(Comment.class); // This is a final class 

Mockito.when(youtubeCommentOne.getSnippet().getUpdatedAt().getValue()).thenReturn(youtubeCommentOneDate); 

} 

什麼我錯在這裏做什麼?

回答

2

分裂鏈方法調用應該工作:

Comment commentMock = PowerMockito.mock(Comment.class); 
CommentThread commentThreadMock = PowerMockito.mock(CommentThread.class); 

when(commentMock.getSnippet()).thenReturn(commentThreadMock); 
when(commentThreadMock.getUpdatedAt()).thenReturn(new DateTime(youtubeCommentOneDate)); 

如果它是你正在尋找沒有什麼,看看this例子。根據這一點,返回深樁可以解決問題。

嘗試模仿評論對象使用標註的Mockito:

@Mock(answer = Answers.RETURNS_DEEP_STUBS) 
Comment youtubeCommentOne; 
+0

感謝您的幫助。那正是我的問題。 – LupoZ

相關問題