Mockito Null Pointer Exception

时间:2018-01-16 17:33:07

标签: android mockito

我正在为我的Adapter类编写Mockito。以下是我的适配器代码

 static class SongAdapterPresenter implements SortedSongSelectionContract.SongAdapterContract.Presenter {

        private List<Song> songs;
        private final Presenter sortedSongSelectionPresenter;
        private final SortedSongSelectionContract.SongAdapterContract.Adapter adapter;
        private SortedSongSelectionContract.SongAdapterContract.SongView selectedSongView;
        private Song selectedSong;

        SongAdapterPresenter(SortedSongSelectionContract.SongAdapterContract.Adapter adapter, SortedSongSelectionContract.Presenter sortedSongSelectionPresenter) {
            this.adapter = adapter;
            this.sortedSongSelectionPresenter = sortedSongSelectionPresenter;
        }

        @Override
        public int getItemCount() {
            return songs != null ? songs.size() : 0;
        }

        @Override
        public void onBindView(SortedSongSelectionContract.SongAdapterContract.SongView songView, int position) {
            Song song = songs.get(position);

            songView.setTitle(song.getName());
            songView.setArtists(song.getArtists());

            List<Genre> genres = song.getGenres();
            int size = genres.size();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < size; i++) {
                builder.append(genres.get(i).getName()).append(",");
            }
            int length = builder.length();
            if (length > 0) {
                builder.deleteCharAt(length - 1);
            }
            songView.setGenres(builder.toString());
            songView.showPlayIcon(true);

            boolean select = isSongEqual(song);
            if (select) {
                selectedSongView = songView;
            }

            songView.showSelectionUi(select);
        }

        @Override
        public void onItemClicked(SortedSongSelectionContract.SongAdapterContract.SongView songView, int position) {
            Song song = songs.get(position);

            if (isSongEqual(song)) {
               return;
            }

            deSelect(song);

            selectedSongView = songView;
            selectedSongView.showSelectionUi(true);
            selectedSong = songs.get(position);
            sortedSongSelectionPresenter.getBus().post(new BusEvents.SongSelected(selectedSong));
        }

        @Override
        public void setSongs(List<Song> songs) {
            this.songs = songs;
            adapter.refresh();
        }

        @Override
        public void deSelect(Song song) {
            if (!isSongEqual(song)) {
                if (selectedSongView != null) {
                    selectedSongView.showSelectionUi(false);
                }
                selectedSong = null;
                selectedSongView = null;
            }
        }

        private boolean isSongEqual(Song song) {
            return !(song == null || selectedSong == null) && (song == selectedSong || selectedSong.getId().equals(song.getId()));
        }
    }
    //endregion

    //region Instance methods
    private void processEvent(Object event) {
        if (event instanceof BusEvents.SongSelected) {
            deSelect(((BusEvents.SongSelected) event).getSong());
        }
    }
    //endregion
}

我正在尝试为适配器类的deSelect方法编写Mockito测试

@RunWith(PowerMockRunner.class)
@PrepareForTest(Log.class)
public class SongAdapterPresenterTest {

    private SortedSongSelectionPresenter.SongAdapterPresenter songAdapterPresenter;

    @Mock
    private SortedSongSelectionContract.SongAdapterContract.Adapter adapter;

    @Mock
    private SortedSongSelectionContract.Presenter presenter;

    @Mock
    private SortedSongSelectionContract.SongAdapterContract.SongView songView;


    private Song song;
    private List<Song> songList;

    @Before
    public void setUp() {
        song = new Song("1", "A", "B");
        songList = new ArrayList<>(1);
        songList.add(song);
        songAdapterPresenter = new SortedSongSelectionPresenter.SongAdapterPresenter(adapter, presenter);
    }


    @Test
    public void getItemCountWithSongListNotNull_returnSongListSize() {
        songAdapterPresenter.setSongs(songList);
        Assert.assertEquals(songList.size(), songAdapterPresenter.getItemCount());
    }

    @Test
    public void getItemCountWithSongListNull_returnIsZero(){
        songAdapterPresenter.setSongs(null);
        Assert.assertEquals(0, songAdapterPresenter.getItemCount());
    }


    @Test
    public void deSelect_SongRemoved(){
        songAdapterPresenter.deSelect(song);
        verify(songView).showSelectionUi(false);

    }

} 

我已经创建了AdapterPresenter的对象和我的View类的Mock对象,但是当我执行deSelect方法时,我一直得到Null Pointer Exception。该方法的第一行是给出错误。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您应该为Mock个对象创建自己的行为,因为默认情况下它们返回null。例如:

when(presenter.someMethod(anyString()).thenReturn(false); 

如果你需要使用Spy调用实际方法的Test对象,或者使用你可以这样做

when(presenter.someMethod(anyString()).thenCallRealMethod();

这称为部分模拟。因此,当您调用presenter.someMethod(“any”)时,将调用Presenter类的实际方法。