Slide 1

Slide 1 text

Testable Android Architecture 11 April 2017 / Chuck Greb / @ecgreb

Slide 2

Slide 2 text

Clean code Software Craftsmanship

Slide 3

Slide 3 text

The only thing that is constant is change. “ - Heraclitus ”

Slide 4

Slide 4 text

Art is anything you can do well. Anything you can do with Quality. “ ” - Robert M. Pirsig

Slide 5

Slide 5 text

→ MVC Model View Controller → MVP Model View Presenter → MVVM Model View ViewModel Clean Architecture Design patterns

Slide 6

Slide 6 text

A brief history of Android development

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Activity

Slide 10

Slide 10 text

Activity Activity Lifecycle System Services

Slide 11

Slide 11 text

Activity EditText Button Activity Lifecycle System Services

Slide 12

Slide 12 text

Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle System Services

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Fragment Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle System Services Fragment Fragment Lifecycle User Input Input Validation

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

A cleaner approach...

Slide 23

Slide 23 text

Activity Logic

Slide 24

Slide 24 text

Activity Logic Data

Slide 25

Slide 25 text

Activity Logic Data View

Slide 26

Slide 26 text

- Phil Karlton There are only two hard problems in computer science: cache invalidation and naming things. “ ”

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Clean Architecture Model View Presenter Controller MVP(C)

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Activity Presenter (Logic) Model (Data) View Controller

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

→ 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

Slide 37

Slide 37 text

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 ”

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

→ 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

Slide 42

Slide 42 text

Test Types

Slide 43

Slide 43 text

Unit Tests JUnit + Mockito (JVM)

Slide 44

Slide 44 text

Mock Service Mock Storage Test Controller Presenter Model Controller JUnit TestRunner Thread run()

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Integration Tests Robolectric

Slide 51

Slide 51 text

Activity Robolectric TestRunner Robolectric Runtime Environment Simulated Input View Button EditText Input Validation Mock Service Mock Storage Presenter Model Thread run()

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

UI Tests Espresso

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

→ Configuration Changes → View Presenters → Legacy Code FAQ Frequently Answered Questions

Slide 57

Slide 57 text

Configuration Changes

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

View Presenters

Slide 61

Slide 61 text

View Model View Presenter Activity Activity Lifecycle System Services User Input Main Presenter View Controller Button EditText Input Validation Main Model

Slide 62

Slide 62 text

Legacy Code

Slide 63

Slide 63 text

→ 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

Slide 64

Slide 64 text

Demo App https://github.com/ecgreb/mvpc

Slide 65

Slide 65 text

→ 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?

Slide 66

Slide 66 text

Merci à vous 11 April 2017 / Chuck Greb / @ecgreb