In this talk, you will learn:
how to take advantage of Mockito and other libraries to write good JVM and Espresso tests
how to use DaggerMock (an open source library available on github https://github.com/fabioCollini/DaggerMock) to avoid boilerplate code in tests
how to test asynchronous RxJava code using a JVM test
Writing reliable tests is not easy for many reasons, especially when asynchronous code is involved. Some libraries can be used to create a testable architecture (for example Dagger and RxJava) and to simplify testing (Mockito, AssertJ and DaggerMock). In this talk we’ll see how to write JVM and Espresso tests with particular attention on how to replace real objects with mocks and how to test asynchronous RxJava code.
17. 17
Integrated tests are a scam
a self-replicating virus that threatens to infect your
code base, your project, and your team with endless
pain and suffering.
J. B. Rainsberger
41. 41
public class MockPresenterTest {
@Rule public ActivityTestRule<UserListActivity> rule =
new ActivityTestRule<>(UserListActivity.class, false, false);
@Rule public MyDaggerMockRule daggerMockRule =
new MyDaggerMockRule();
@Mock UserListPresenter presenter;
@Test
public void testOnCreate() {
rule.launchActivity(null);
onView(withId(R.id.text)).check(matches(withText("")));
verify(presenter).reloadUserList();
}
}
Activity
Presenter
Interactor
Retrofit
Service
42. 1.void method that uses
RxJava schedulers
2.method that returns a
synchronous RxJava object
3.method that returns an
asynchronous RxJava object
Testing RxJava code
76. 1.void method that uses
RxJava schedulers
2.method that returns a
synchronous RxJava object
3.method that returns an
asynchronous RxJava object
Testing RxJava code
trampoline
scheduler
blockingGet
TestScheduler
& TestObserver
77. 77
Wrapping up
1.JVM tests isolate failures and are fast and
reliable
2.Using DaggerMock testing boilerplate code can
be reduced
3.RxJava asynchronous code can be tested using
TestObserver and TestScheduler