2016-05-12 46 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