I have been wanting to learn the Model View Presenter pattern for a while, and was super excited to get Michael Cameron to give a talk on it at GDG Boulder last night.
View Interface
I have this vague notion that MVP is good for JUnit testing, and I finally figured out why. The secret, the missing link, is View Interface, which is not a part of the MVP acronym. View Interface defines the contract between the view and the presenter, allowing you to mock one while testing the other.
With View Interface, you can compartmentalize your Android UI code into the view (typically a Fragment or Activity), which communicates with your presenter solely through the View Interface. This separation allows you to mock your View Interface with zero Android code, which means you can test your presenter with JUnit on the JVM.
Mocking via build flavors
Mocking is essential for hermetic testing, but people are often intimated by dependency injection frameworks like Dagger. If you prefer, you can provide mocks to your test using build flavors. An excellent technique!
Mockito
Mockito is a great mocking framework with a fluent API. One very nice feature is mocking via annotations. To use that, annotate the fields you want to mock with @Mock, and initialize them all with MockitoAnnotations.initMocks() in your @Before function.
@Mock
private MyThing myThing;
@Mock
private OtherThing otherThing;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
This is equivalent to:
private MyThing myThing;
private OtherThing otherThing;
@Before public void setUp() throws Exception {
myThing = Mockito.mock(MyThing.class);
otherThing = Mockito.mock(OtherThing.class);
}
Model View Presenter for Android by @Darxval at @gdgboulder #Sketchnotes pic.twitter.com/T7QX0FTPzQ
— Chiu-Ki Chan (@chiuki) November 6, 2015
Slides: http://www.slideshare.net/DarxVal/model-view-presenter-presentation
Codelab
Google has published the excellent Android Testing Codelab going through an app with the MVP architecture. You can see JUnit and Espresso testing in action, mocks via build flavors, and various other techniques. Go check it out!

