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

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?

Chuck Greb

April 11, 2017
Tweet

More Decks by Chuck Greb

Other Decks in Technology

Transcript

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

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

    → MVVM Model View ViewModel Clean Architecture Design patterns
  3. Fragment Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle

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

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

    Fragment Fragment Lifecycle User Input setRetainInstance(true) Intent Parcelable Extras EditText Button Input Validation
  6. 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
  7. - Phil Karlton There are only two hard problems in

    computer science: cache invalidation and naming things. “ ”
  8. Web Service Storage Activity Activity Lifecycle System Services User Input

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

    Presenter View Model Controller Button EditText Input Validation Thread start()
  10. → 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
  11. 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 ”
  12. 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
  13. → 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
  14. Activity Robolectric TestRunner Robolectric Runtime Environment Simulated Input View Button

    EditText Input Validation Mock Service Mock Storage Presenter Model Thread run()
  15. Web Service Storage Activity Activity Lifecycle System Services Presenter View

    Model Controller Button EditText Thread Input Validation start() Android JUnit4 TestRunner
  16. Web Service Storage Activity Activity Lifecycle System Services User Input

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

    Presenter View Model Controller Button EditText Thread Input Validation start() ViewState Manager (VSM)
  18. View Model View Presenter Activity Activity Lifecycle System Services User

    Input Main Presenter View Controller Button EditText Input Validation Main Model
  19. → 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
  20. → 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?