2016-05-12 7 views
1

私がチェーンメソッド呼び出しをモックすると、nullpointer例外が発生する。Powermockチェーンメソッドがfinalクラスで呼び出す

私のコードは次のように探しています:

@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アノテーションを使用してモックCommentオブジェクトへ

試してみてください。助けを

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

感謝。それはまさに私の問題でした。 – LupoZ

関連する問題