Skip to content

Instantly share code, notes, and snippets.

@dazza5000
Last active April 20, 2019 18:59
Show Gist options
  • Save dazza5000/5748927f3181a22f6578176250c5e088 to your computer and use it in GitHub Desktop.
Save dazza5000/5748927f3181a22f6578176250c5e088 to your computer and use it in GitHub Desktop.
Events Presenter Test
public class EventsPresenterTest {
private static List<Event> EVENTS = new ArrayList<>();
@Mock
EventsRepository eventsRepository;
@Mock
EventsContract.View eventsView;
private EventsPresenter eventsPresenter;
/**
* {@link ArgumentCaptor} is a powerful Mockito API to capture argument values and use them to
* perform further actions or assertions on them.
*/
@Captor
private ArgumentCaptor<EventsDataSource.LoadEventsCallback> loadEventsCallbackCaptor;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
eventsPresenter = new EventsPresenter(eventsRepository, eventsView);
// Test data
EVENTS.add(
new Event("1", "Pizza Fest", "Pizza Everywhere", 33928672270000L,
"www.pizza.com", "pizza", true));
EVENTS.add(
new Event("2", "Beer", "Duff Everywhere", 33928672270777L,
"www.duffman.com", "beer", true));
}
@Test
public void loadAllEventsFromRepositoryAndLoadIntoView() {
eventsPresenter.loadEvents();
// Callback is captured and invoked with stubbed events
verify(eventsRepository).getEvents(loadEventsCallbackCaptor.capture());
loadEventsCallbackCaptor.getValue().onEventsLoaded(EVENTS);
//Verify that view is given a list of one event from the presenter
ArgumentCaptor<List> showEventsArgumentCaptor = ArgumentCaptor.forClass(List.class);
verify(eventsView).showEvents(showEventsArgumentCaptor.capture());
assertTrue(showEventsArgumentCaptor.getValue().size() == 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment