我正在尝试使用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
}
我想为onBindView编写测试 以下是我的测试类
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 testonBindView() {
songAdapterPresenter.onBindView(songView, 1);
verify(songView).showSelectionUi(true);
}
@Test
public void deSelect_SongRemoved(){
songAdapterPresenter.deSelect(song);
verify(songView).showSelectionUi(false);
}
}
我创建了Mock对象视图和我的PresenterAdapter类的对象。我没有得到导致错误的原因。 当我执行onBindView Test时,我不断获得NullPointer 任何帮助将不胜感激。
答案 0 :(得分:0)
你永远不会调用setSongs
,这就是你得到NullPointerException的原因
另请注意,您的songList
只有一个元素。
将您的代码更改为:
@Test
public void testonBindView() {
songAdapterPresenter.setSongs(songList);
songAdapterPresenter.onBindView(songView, 0);
verify(songView).showSelectionUi(true);
}