Skip to content

Testing

josheroff edited this page Jan 31, 2017 · 1 revision

Testing

One of the key reasons why a UI architecture like this is so useful is due to the ease of testing. Most of the tests will be able to be pure JUnit tests, and Espresso tests can remain more "black-box" tests with no knowledge of the underlying architecture.

Unit Tests

Here's an example of a unit test for a sample Interactor implementation:

public class ButtonInteractorTest {

    @Mock
    FakeDb fakeDb;

    private ButtonInteractor buttonInteractor;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        buttonInteractor = new ButtonInteractor(fakeDb);
    }

    @Test
    public void incrementNumberBy_updatesFakeDbNumber() {
        buttonInteractor.incrementNumberBy(1);
        verify(fakeDb).updateNumberBy(1);
    }
}

This is a very simple unit test, but if done correctly, most of your Privvy implementation unit tests should be able to be as clean and simple as this. External dependencies can be mocked and each method can be tested with all of the possible edge cases accounted for.

Instrumentation Tests

Since all of our individual Privvy pieces can be individually unit tested, your Espresso tests should be able to be much simpler as well. These tests should focus on testing that the views are displayed correctly, user interactions are handled properly, and you are routed to the right areas of your application.

Here's an example of a simple Espresso test class you may have:

public class ExampleInstrumentedTest {

    @Rule
    public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class, false, false);

    @Before
    public void setup() {
        TestSampleApplication.component.fakeDb().setNumberValue(0);
        activityTestRule.launchActivity(null);
    }

    @Test
    public void clickPlus_incrementsNumber() {
        onView(withText("0")).check(matches(isDisplayed()));
        onView(withText("+")).perform(click());
        onView(withText("1")).check(matches(isDisplayed()));
    }

    @Test
    public void clickMinus_decrementsNumber() {
        onView(withText("0")).check(matches(isDisplayed()));
        onView(withText("-")).perform(click());
        onView(withText("-1")).check(matches(isDisplayed()));
    }

    @Test
    public void longPressNumber_navigatesToNumberList() {
        onView(withText("Back")).check(doesNotExist());

        onView(withText("0")).check(matches(isDisplayed()));
        onView(withText("+")).perform(click());
        onView(withText("1")).perform(longClick());

        onView(withText("Back")).check(matches(isDisplayed()));
    }
}
Clone this wiki locally