Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testable Android Architecture

613633962e5ab27cf572e7699e43d368?s=47 Chuck Greb
September 29, 2016

Testable Android Architecture

So many testing tools exist for Android including JUnit, Mockito, Robolectric, and Espresso. But how can you design your application to leverage each one most effectively? This talk introduces a modified version of the Model View Presenter (MVP) architecture to organize your code to be more flexible, maintainable, and testable.

613633962e5ab27cf572e7699e43d368?s=128

Chuck Greb

September 29, 2016
Tweet

Transcript

  1. Testable Android Architecture 29 Sept 2016 / Chuck Greb

  2. Clean code Software Craftsmanship

  3. Art is anything you can do well. Anything you can

    do with Quality. “ ” - Robert M. Pirsig
  4. None
  5. Clean Architecture MVC <> MVP <> MVVM

  6. In the beginning

  7. Activity

  8. Activity Activity Lifecycle System Services

  9. Activity EditText Button Activity Lifecycle System Services

  10. Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle System

    Services
  11. Fragment Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle

    System Services Fragment Fragment Lifecycle
  12. Fragment Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle

    System Services Fragment Fragment Lifecycle User Input Input Validation
  13. Fragment Activity Web Service Storage AsyncTask Activity Lifecycle System Services

    Fragment Fragment Lifecycle User Input setRetainInstance(true) EditText Button Input Validation
  14. Fragment Activity Web Service Storage AsyncTask Activity Lifecycle System Services

    Fragment Fragment Lifecycle User Input setRetainInstance(true) Intent Parcelable Extras EditText Button Input Validation
  15. Web Service Storage Activity Fragment Fragment AsyncTask setRetainInstance(true) Activity Lifecycle

    Fragment Lifecycle System Services User Input Intent ActivityInstrumentati onTestCase2 Parcelable Extras EditText Button Input Validation
  16. None
  17. There has to be a better way

  18. Activity Logic

  19. Activity Logic Data

  20. Activity Logic Data View

  21. View (Activity) Controller (Logic) Model (Data) View

  22. View (Activity) Presenter (Logic) Model (Data) View

  23. View (Activity) ViewModel (Logic) Model (Data) View

  24. Controller (Activity) Subcontroller (Logic) Model (Data) View

  25. - Phil Karlton There are two hard things in computer

    science: cache invalidation, naming things, [and errors off by one.] “ ”
  26. Clean Architecture Model View Presenter Controller MVP(C)

  27. Controller (Activity) Presenter (Logic) Model (Data) View

  28. Activity Presenter (Logic) Model (Data) View Controller

  29. Web Service Storage Activity Activity Lifecycle System Services User Input

    Presenter View Model Controller Button EditText Thread Input Validation
  30. Web Service Storage Activity Activity Lifecycle System Services User Input

    Presenter View Model Controller Thread Button EditText Input Validation
  31. Mock Service Mock Storage Test Controller Presenter Model Controller Thread

    JUnit TestRunner
  32. A test is not a unit test if: → It

    talks to the database → It communicates across the network → It touches the file system → It can't run at the same time as any of your other unit tests → You have to do special things to your environment (such as editing config files) to run it. Michael Feathers, 2005 A Set of Unit Test Rules
  33. Mock Service Mock Storage Test Controller Presenter Model Controller Thread

    JUnit TestRunner
  34. Activity Robolectric TestRunner Robolectric Runtime Environment Simulated Input Stub Presenter

    View Button EditText Input Validation
  35. Demo App https://github.com/ecgreb/mvpc

  36. Eraser Map

  37. None
  38. Start where you are