38
Testable Android Architecture 29 Sept 2016 / Chuck Greb

Testable Android Architecture

Embed Size (px)

Citation preview

Testable Android Architecture

29 Sept 2016 / Chuck Greb

Clean code

Software Craftsmanship

Art is anything you can do well. Anything you can do with Quality.

”- Robert M. Pirsig

Clean Architecture

MVC <> MVP <> MVVM

In the beginning

Activity

Activity

ActivityLifecycle

SystemServices

Activity

EditText

ButtonActivity

Lifecycle

SystemServices

Activity

EditText

Button

Web Service

Storage

AsyncTask

ActivityLifecycle

SystemServices

Fragment

Activity

EditText

Button

Web Service

Storage

AsyncTask

ActivityLifecycle

SystemServices

Fragment

FragmentLifecycle

Fragment

Activity

EditText

Button

Web Service

Storage

AsyncTask

ActivityLifecycle

SystemServices

Fragment

FragmentLifecycle

UserInput

InputValidation

Fragment

Activity

Web Service

Storage

AsyncTask

ActivityLifecycle

SystemServices

Fragment

FragmentLifecycle

UserInput

setRetainInstance(true)

EditText

Button

InputValidation

Fragment

Activity

Web Service

Storage

AsyncTask

ActivityLifecycle

SystemServices

Fragment

FragmentLifecycle

UserInput

setRetainInstance(true)

Intent

ParcelableExtras

EditText

Button

InputValidation

Web Service

Storage

Activity

Fragment

Fragment

AsyncTask

setRetainInstance(true)

ActivityLifecycle

FragmentLifecycle

SystemServices

UserInput

Intent

ActivityInstrumentationTestCase2

ParcelableExtras

EditText

Button

InputValidation

There has to be a better way

Activity Logic

Activity Logic

Data

Activity Logic

DataView

View(Activity)

Controller(Logic)

Model(Data)

View

View(Activity)

Presenter(Logic)

Model(Data)

View

View(Activity)

ViewModel(Logic)

Model(Data)

View

Controller(Activity)

Subcontroller(Logic)

Model(Data)

View

- Phil Karlton

There are two hard things in computer science: cache invalidation, naming things, [and errors off by one.]

Clean Architecture

Model ViewPresenter Controller

MVP(C)

Controller(Activity)

Presenter(Logic)

Model(Data)

View

Activity Presenter(Logic)

Model(Data)

View

Controller

Web Service

Storage

ActivityActivityLifecycle

SystemServices

UserInput

Presenter

View Model

Controller

Button

EditText

Thread

Input Validation

Web Service

Storage

ActivityActivityLifecycle

SystemServices

UserInput

Presenter

View Model

Controller

ThreadButton

EditText

Input Validation

Mock Service

MockStorage

TestController

Presenter

Model

Controller

Thread

JUnitTestRunner

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

Mock Service

MockStorage

TestController

Presenter

Model

Controller

Thread

JUnitTestRunner

ActivityRobolectricTestRunner

RobolectricRuntime

Environment

SimulatedInput

StubPresenter

View

Button

EditText

Input Validation

Demo App

https://github.com/ecgreb/mvpc

Eraser Map

Start where you are