Testable Android Architecture (Android Makers France)

Testable Android Architecture (Android Makers France)

Today we have a variety of automated testing tools available for Android including JUnit, Mockito, Robolectric, Espresso, UI Automator and more.

But how can we design our applications to leverage each tool most effectively and at the same time ensure our code is readable, flexible, and maintainable?

613633962e5ab27cf572e7699e43d368?s=128

Chuck Greb

April 11, 2017
Tweet

Transcript

  1. Testable Android Architecture 11 April 2017 / Chuck Greb /

    @ecgreb
  2. Clean code Software Craftsmanship

  3. The only thing that is constant is change. “ -

    Heraclitus ”
  4. Art is anything you can do well. Anything you can

    do with Quality. “ ” - Robert M. Pirsig
  5. → MVC Model View Controller → MVP Model View Presenter

    → MVVM Model View ViewModel Clean Architecture Design patterns
  6. A brief history of Android development

  7. https://en.wikipedia.org/wiki/Android_version_history

  8. None
  9. Activity

  10. Activity Activity Lifecycle System Services

  11. Activity EditText Button Activity Lifecycle System Services

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

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

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

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

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

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

    Fragment Lifecycle System Services User Input Intent Test Case Parcelable Extras EditText Button Input Validation
  18. None
  19. None
  20. None
  21. None
  22. A cleaner approach...

  23. Activity Logic

  24. Activity Logic Data

  25. Activity Logic Data View

  26. - Phil Karlton There are only two hard problems in

    computer science: cache invalidation and naming things. “ ”
  27. View (Activity) Controller (Logic) Model (Data) View

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

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

  30. Clean Architecture Model View Presenter Controller MVP(C)

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

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

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

    Presenter View Model Controller Button EditText Thread Input Validation start()
  34. Web Service Storage Activity Activity Lifecycle System Services User Input

    Presenter View Model Controller Button EditText Input Validation Thread start()
  35. None
  36. → UI end-to-end tests through the user interface. Simulates actual

    usage of the app. Often slow, expensive, and brittle. → Service provide many of the advantages of end-to-end tests but avoid many of the complexities of dealing with UI frameworks. → Unit the smallest testable parts of an app are individually and independently exercised for proper operation. Test Pyramid
  37. If you get a failure in a high level test,

    not just do you have a bug in your functional code, you also have a missing or incorrect unit test. “ - Martin Fowler ”
  38. 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 Unit Test Rules
  39. None
  40. None
  41. → UI end-to-end tests run on an emulator or device

    using the Espresso framework. → Integration using Robolectric tests can be run in the local JVM against the real Android JARs mocking only UI and system hardware components. → Unit use JUnit and Mockito plus a special mockable version of the Android JAR to test app code in isolation in the local JVM. Android Test Pyramid
  42. Test Types

  43. Unit Tests JUnit + Mockito (JVM)

  44. Mock Service Mock Storage Test Controller Presenter Model Controller JUnit

    TestRunner Thread run()
  45. None
  46. None
  47. None
  48. None
  49. None
  50. Integration Tests Robolectric

  51. Activity Robolectric TestRunner Robolectric Runtime Environment Simulated Input View Button

    EditText Input Validation Mock Service Mock Storage Presenter Model Thread run()
  52. None
  53. UI Tests Espresso

  54. Web Service Storage Activity Activity Lifecycle System Services Presenter View

    Model Controller Button EditText Thread Input Validation start() Android JUnit4 TestRunner
  55. None
  56. → Configuration Changes → View Presenters → Legacy Code FAQ

    Frequently Answered Questions
  57. Configuration Changes

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

    Presenter View Model Controller Button EditText Thread Input Validation start()
  59. Web Service Storage Activity Activity Lifecycle System Services User Input

    Presenter View Model Controller Button EditText Thread Input Validation start() ViewState Manager (VSM)
  60. View Presenters

  61. View Model View Presenter Activity Activity Lifecycle System Services User

    Input Main Presenter View Controller Button EditText Input Validation Main Model
  62. Legacy Code

  63. → Code that does not have tests was probably not

    written to be testable → Do not set aside dedicated time to “refactor” and “improve test coverage” → Test any new code that is added → Test any code around new code that is added → Use seams to break dependencies, decouple components, and modularize your code base Legacy Code Code without tests
  64. Demo App https://github.com/ecgreb/mvpc

  65. → Clean architecture can help your code be more flexible,

    maintainable, and testable. → Choose an architecture appropriate to your use case. → Write unit, integration, and UI tests that work in concert with your architecture. → Don’t be overly dogmatic-- software development is an art and a science. Conclusion Which architecture and testing tools should I use?
  66. Merci à vous 11 April 2017 / Chuck Greb /

    @ecgreb