Slide 1

Slide 1 text

Automated Testing for Modern Android Applications

Slide 2

Slide 2 text

Andy Dyer +AndrewDyer @dammitandy

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Making Android apps testable

Slide 5

Slide 5 text

Dynamic languages Tests shouldn't drive implementation.

Slide 6

Slide 6 text

Static languages You know nothing.

Slide 7

Slide 7 text

Making Android apps testable 1. Dependency injection 2. Mocking & stubbing 3. Unit & integration tests

Slide 8

Slide 8 text

Dependency injection

Slide 9

Slide 9 text

Dependency injection • Classes receive dependencies, don’t have to know where to find them or how to create them • Swap components for mocks in tests • Use different components for different build flavors, etc.

Slide 10

Slide 10 text

Basic dependency injection public class Beer implements Reinheitsgebot { Water water; Barley barley; Hops hops; public Beer(Water water, Barley barley, Hops hops) { this.water = water; this.barley = barley; this.hops = hops; } }

Slide 11

Slide 11 text

Dagger A Java dependency injection library

Slide 12

Slide 12 text

Dagger • Defining dependencies at compile time avoids reflection at runtime • Compiler validates components, modules, and injections • Dagger 2 is currently in alpha, but already being used by Google in production apps google.github.io/dagger/

Slide 13

Slide 13 text

Dagger Modules @Module public class MyModule { @Provides @Singleton public MyService provideMyService() { return new MyService(); } }

Slide 14

Slide 14 text

Dagger Components @Component(modules = MyModule.class) public interface Graph { void inject(Activity activity); void inject(Fragment fragment); public final static class Initializer { public static Graph init(boolean mockMode) { return Dagger_Graph.builder().build(); } } }

Slide 15

Slide 15 text

Dagger Object Graph public class MyApplication extends Application { @Getter static DemoApplication instance; @Getter Graph graph; @Override public void onCreate() { super.onCreate(); instance = this; graph = Graph.Initializer.init(false); } public void setMockMode(boolean useMock) { graph = Graph.Initializer.init(useMock); } }

Slide 16

Slide 16 text

Dagger dependency injection public class MyFragment extends Fragment { @Inject MyService service; @Override public void onViewCreated(View view, Bundle savedInstanceState) { MyApplication.getInstance().getGraph().inject(this); service.getMyData(); } }

Slide 17

Slide 17 text

Learning more about Dagger • Jake Wharton - Dependency Injection with Dagger 2 parleys.com/play/5471cdd1e4b065ebcfa1d557 • Gregory Kick - Dagger 2: A New Type of Dependency Injection youtube.com/watch?v=oK_XtfXPkqw

Slide 18

Slide 18 text

Mocking & Stubbing

Slide 19

Slide 19 text

Mocking & Stubbing • Substitute runtime implementation for something that can be predictably tested in isolation • Verify behavior

Slide 20

Slide 20 text

A Java mocking library

Slide 21

Slide 21 text

Mockito • Mock/stub dependencies and function return values • Inject mocks to validate behavior in tests • Use included Hamcrest matchers for clear, readable tests code.google.com/p/mockito/

Slide 22

Slide 22 text

Using Mockito // create mock MyClass mocked = mock(MyClass.class); // specify behavior when(mocked.doSomething()).thenReturn(somethingElse); // verify method calls verify(mocked).getMyData(anyInt(), anyString());

Slide 23

Slide 23 text

Using Mockito // capture arguments ArgumentCaptor captor = ArgumentCaptor.forClass(Callback.class); verify(authenticationService).login(anyString(), anyString(), captor.capture()); // simulate error conditions, etc. captor.getValue().failure(RetrofitError.unexpectedError( "Invalid password", new Exception()));

Slide 24

Slide 24 text

Unit & Integration Tests

Slide 25

Slide 25 text

Tests or it didn't happen

Slide 26

Slide 26 text

Unit testing public class BeerTest extends InstrumentationTestCase { @Inject Beer beer; @Override protected void setUp() throws Exception { MyApplication.getInstance().getGraph().inject(this); } public void testBeerIsGood() { assertTrue(beer.isGood()); } }

Slide 27

Slide 27 text

An Android UI testing library

Slide 28

Slide 28 text

Espresso • Handles activity creation & state sync • Simple, concise API • Really fast! code.google.com/p/android-test-kit*

Slide 29

Slide 29 text

UI testing with Espresso public class MyActivityTest extends ActivityInstrumentationTestCase2 { public MyActivityTest() { super(MyActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); getActivity(); // trigger activity launch } public void testInvalidEmailShowsError() { onView(withId(R.id.email)).perform(typeText("abc"), closeSoftKeyboard()); onView(withId(R.id.email_sign_in_button)).perform(click()); onView(withId(R.id.email)).check(matches(withError( getActivity().getString(R.string.error_invalid_email)))); } }

Slide 30

Slide 30 text

Sample application • Dagger object graph • Retrofit API with sample request • Lombok & Android Studio plugin • Login activity • Activity with fragment to make API request and display data

Slide 31

Slide 31 text

Testing the login activity • Simulate data entry and taps to validate UI functionality • Simulate an error without making a network request

Slide 32

Slide 32 text

Testing the main activity • Verify the RecyclerView contains data • Simulate taps to validate the appropriate URLs are loaded in a WebView

Slide 33

Slide 33 text

Testing the API • Use mock API response loaded from text file to validate JSON parsing

Slide 34

Slide 34 text

Demo

Slide 35

Slide 35 text

Known issues • Some tests may fail on older devices/emulators such as those running < API 18. Hopefully this will be fixed soon by an update to the test support library.

Slide 36

Slide 36 text

Additional resources • Bryan Stern/Circle Engineering - Instrumentation Testing with Dagger, Mockito, and Espresso engineering.circle.com/instrumentation-testing-with-dagger- mockito-and-espresso

Slide 37

Slide 37 text

Conclusion Using dependency injection, mocking, and automated testing tools helps us build better apps. Working together as a community, we can make testing even easier.

Slide 38

Slide 38 text

Questions?

Slide 39

Slide 39 text

Slides speakerdeck.com/abdyer/mce2015-automated-testing-for- modern-android-applications Code github.com/abdyer/android-test-demo/releases/tag/ mceconf-2015 +AndrewDyer @dammitandy