Share
Embed
Testing By Design
Building an efficient and maintainable test suite for an app is challenging. Design patterns like MVP or MVVM help decouple responsibilities so that it becomes easier to write testable code. What is the next level though? How can we get more out of our tests? How can we achieve the same results while writing less code for testing? How can we make tests resilient to minor changes? What if we take all this into account from the very beginning instead of making testing an afterthought? What if our architecture is built for testing by design?
In this talk we are going to discuss how a testing-first architecture such as MVI helps us to write and maintain efficient tests.
Hannes Dorfmann
May 22, 2019
Transcript
145 slides
-
Challenges - Isolation of side effects - Unreliable slow layers:
network, disk access, db access - Every code change affects tests 12 -
Uncle Bob says: Keep the test code to the same
level of quality as the production code. Test code is not throw-away code. 15 -
@Test fun newTodoItemIsShownInTodoList() { onView(withId(R.id.newItem)).perform(click()) onView(withId(R.id.step1Title)).perform(typeText("Another Item")) onView(withId(R.id.button)).perform(click()) onView(withId(R.id.create)).perform(click()) onView(withText("Another
Item")).check(matches(isDisplayed())) } 16 -
Robot pattern - influenced by Martin Fowler’s PageObject pattern -
Embraced by kotlin DSL syntax 18 -
Robot pattern. Key principles - Let robot do what user
can do - Verify what user would see 19 -
@Test fun newTodoItemIsShownInTodoList() { todoList { clickCreateTodoItem() createItem { enterTitle("Another
Item") pressNext() } } } 25 -
@Test fun newTodoItemIsShownInTodoList() { todoList { clickCreateTodoItem() createItem { enterTitle("Another
Item") pressNext() pressSave() } } } 26 -
Robot pattern. Advantages - No need to change tests if
the flow doesn’t change - Fun to read - Easy to extend 27 -
Problems with “traditional” Robot Pattern - In which state is
the Robot starting? - Assertions hidden - Doesn’t read like a specification 28 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) todoList { } 29 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { todoList { } } 30 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { } } 31 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState() } } 32 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState } } 33 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState } } val assertLoadingState: Unit get() = ... 34 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState } } 35 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList } } 36 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() } } 37 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { } } } 38 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") } } } 39 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState } } } 40 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() } } } 41 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState } } } 42 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() } } } 43 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState } } } 44 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } } } 45 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertContentState + itemToAdd } } 46 -
@Test fun markAsDoneAndNotDone() { val prefilled = listOf( TodoItem("1", "First
Item", false), TodoItem("2", "Second item", true) ) given { prefilledTodoItems = prefilled } } 47 -
@Test fun markAsDoneAndNotDone() { val prefilled = listOf( TodoItem("1", "First
Item", false), TodoItem("2", "Second item", true) ) given { prefilledTodoItems = prefilled todoList { assertLoadingState assertContentState + prefilled } } } 48 -
@Test fun markAsDoneAndNotDone() { val prefilled = listOf( TodoItem("1", "First
Item", false), TodoItem("2", "Second item", true) ) given { prefilledTodoItems = prefilled todoList { assertLoadingState assertContentState + prefilled clickFirstItem() } } } 49 -
@Test fun markAsDoneAndNotDone() { val prefilled = listOf( TodoItem("1", "First
Item", false), TodoItem("2", "Second item", true) ) given { prefilledTodoItems = prefilled todoList { assertLoadingState assertContentState + prefilled clickFirstItem() assertContentStateWithFirstItemDone } } } 50 -
@Test fun markAsDoneAndNotDone() { val prefilled = listOf( TodoItem("1", "First
Item", false), TodoItem("2", "Second item", true) ) given { prefilledTodoItems = prefilled todoList { assertLoadingState assertContentState + prefilled clickFirstItem() assertContentStateWithFirstItemDone clickFirstItem() } } } 51 -
@Test fun markAsDoneAndNotDone() { val prefilled = listOf( TodoItem("1", "First
Item", false), TodoItem("2", "Second item", true) ) given { prefilledTodoItems = prefilled todoList { assertLoadingState assertContentState + prefilled clickFirstItem() assertContentStateWithFirstItemDone clickFirstItem() assertContentStateWithFirstItemNotDone } } } 52 -
@Test fun navigateBackAndForthInCreateItemWizard() { given { initialState = SummaryState("Some item")
createItem { assertSummaryState pressBack() assertEnterTitleState pressNext() assertSummaryState } } } 53 -
State Based Architecture - Business Logic based on state machines
- Single source of truth - State gets “rendered” on screen - Atomic UI updates - Push, not pull 55 -
interface TodoRepository { fun getAll() : Observable<List<TodoItem>> fun add(item :
TodoItem) fun update(item : TodoItem) } 58 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine 60 -
class TodoListStateMachine @Inject constructor(private val repository: TodoRepository) { val state:
Observable<State> fun input(action: Action) { } } 61 -
class TodoListStateMachine @Inject constructor(private val repository: TodoRepository) { sealed class
State { object Loading : State() data class Content(val items: List<TodoItem>) : State() object Error : State() } val state: Observable<State> fun input(action: Action) { } } 62 -
class TodoListStateMachine @Inject constructor(private val repository: TodoRepository) { sealed class
State { object Loading : State() data class Content(val items: List<TodoItem>) : State() object Error : State() } val state: Observable<State> = repository.getAll() .map { State.Content(it) as State } .onErrorReturn { State.Error } .startWith(State.Loading) fun input(action: Action) { } } 63 -
class TodoListStateMachine @Inject constructor(private val repository: TodoRepository) { sealed class
State { object Loading : State() data class Content(val items: List<TodoItem>) : State() object Error : State() } val state: Observable<State> = ... fun input(action: Action) { } } 64 -
class TodoListStateMachine @Inject constructor(private val repository: TodoRepository) { sealed class
State { object Loading : State() data class Content(val items: List<TodoItem>) : State() object Error : State() } sealed class Action { data class ToggleTodoItemDoneAction(val item: TodoItem) : Action() data class DeleteTodoItemAction(val item: TodoItem) : Action() } val state: Observable<State> = ... fun input(action: Action) { } } 65 -
class TodoListStateMachine @Inject constructor(private val repository: TodoRepository) { sealed class
State { object Loading : State() data class Content(val items: List<TodoItem>) : State() object Error : State() } sealed class Action { data class ToggleTodoItemDoneAction(val item: TodoItem) : Action() data class DeleteTodoItemAction(val item: TodoItem) : Action() } val state: Observable<State> = ... fun input(action: Action) { when (action) { ... } } } 66 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine 67 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel 68 -
class TodoListViewModel @Inject constructor( private val stateMachine: TodoListStateMachine ) :
ViewModel() { val state = MutableLiveData<State>() fun input(action: Action) { } } 69 -
class TodoListViewModel @Inject constructor( private val stateMachine: TodoListStateMachine ) :
ViewModel() { private val disposable: Disposable = stateMachine.state .subscribeOn(Schedulers.io()) .subscribe { state.postValue(it) }, val state = MutableLiveData<State>() fun input(action: Action) { stateMachine.input(action) } override fun onCleared() { disposable.dispose() } } 70 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel pure functions 71 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment pure functions 72 -
class TodoListViewFragment : Fragment() { @Inject lateinit var viewModel :
TodoListViewModel @Inject lateinit var viewBinder: TodoListViewBinder override fun onCreateView(inflater: LayoutInflater, c: ViewGroup?, b: Bundle?): View? = inflater.inflate(R.layout.fragment_todolist, c, false) override fun onStart() { super.onStart() viewModel.state.observe(this, Observer { viewBinder.render(it) }) viewBinder.actionListener = viewModel::input } } 73 -
class TodoListViewFragment : Fragment() { @Inject lateinit var viewModel :
TodoListViewModel @Inject lateinit var viewBinder: TodoListViewBinder override fun onCreateView(inflater: LayoutInflater, c: ViewGroup?, b: Bundle?): View? = inflater.inflate(R.layout.fragment_todolist, container, false) override fun onStart() { super.onStart() viewModel.state.observe(this, Observer { viewBinder.render(it) }) viewBinder.actionListener = viewModel::input } } 74 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment 75 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder 76 -
open class TodoListViewBinder(private val root: View) { lateinit var actionListener:
(Action) -> Unit private val adapter = TodoListAdapter({ actionListener(it) }) private val recyclerView = root.findViewById<RecyclerView>(R.id.recyclerView) private val error = root.findViewById<View>(R.id.error) private val loading = root.findViewById<View>(R.id.loading) open fun render(state: State) = when (state) { TodoListStateMachine.State.Loading -> { loading.visible() ... } TodoListStateMachine.State.Error -> { error.visible() ... } is TodoListStateMachine.State.Content -> { recyclerView.visible() adapter.items = state.items adapter.notifyDataSetChanged() ... } } } 77 -
open class TodoListViewBinder(private val root: View) { lateinit var actionListener:
(Action) -> Unit private val adapter = TodoListAdapter({ actionListener(it) }) private val recyclerView = root.findViewById<RecyclerView>(R.id.recyclerView) private val error = root.findViewById<View>(R.id.error) private val loading = root.findViewById<View>(R.id.loading) open fun render(state: State) = when (state) { TodoListStateMachine.State.Loading -> { loading.visible() ... } TodoListStateMachine.State.Error -> { error.visible() ... } is TodoListStateMachine.State.Content -> { recyclerView.visible() adapter.items = state.items adapter.notifyDataSetChanged() ... } } } 78 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder 79 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder 80 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder TodoListRobot 81 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions TodoListRobot 82 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions ? TodoListRobot 83 -
@Test fun test1() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll()
).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } 85 -
@Test fun test1() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll()
).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } @Test fun test2() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll() ).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } 86 -
@Test fun test1() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll()
).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } @Test fun test2() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll() ).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } @Test fun test3() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll() ).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } @Test fun test4() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll() ).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } @Test fun test5() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll() ).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } @Test fun test6() { val repository = Mockito.mock(TodoRepository::class.java) `when`( repository.getAll() ).thenReturn( ... ) verify( repository, times(1) ).addItem(someItem) } @Test fun test7() { val repository = Mockito.mock `when`( repository.getAll() ) verify( repository, times(1) } @Test fun test8() { val repository = Mockito.moc `when`( repository.getAll() verify( repository, times(1) } @Test fun test9() { val repository = Mockito.m `when`( repository.getAll( verify( repository, times( } 87 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions ? TodoListRobot 91 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions InMemoryTodoRepository TodoListRobot 92 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions InMemoryTodoRepository TodoListRobot Config 93 -
@Test fun markAsDoneAndNotDone() { val prefilled = listOf( TodoItem("1", "First
Item", false), TodoItem("2", "Second item", true) ) given { prefilledTodoItems = prefilled ... todoList { ... } } } 94 -
fun given(configBlock: Config.() -> Unit) { val config = Config(activityRule)
configBlock(config) } 95 -
fun given(configBlock: Config.() -> Unit) { val config = Config(activityRule)
configBlock(config) } class Config { var prefilledTodoItems = emptyList<TodoItem>() } 96 -
fun given(configBlock: Config.() -> Unit) { val config = Config(activityRule)
configBlock(config) } class Config { var prefilledTodoItems = emptyList<TodoItem>() fun todoList(block: TodoListRobot.() -> Unit) { } } 97 -
fun given(configBlock: Config.() -> Unit) { val config = Config(activityRule)
configBlock(config) } class Config { var prefilledTodoItems = emptyList<TodoItem>() fun todoList(block: TodoListRobot.() -> Unit) { val todoRepository : InMemoryTodoRepository = ... } } 98 -
fun given(configBlock: Config.() -> Unit) { val config = Config(activityRule)
configBlock(config) } class Config { var prefilledTodoItems = emptyList<TodoItem>() fun todoList(block: TodoListRobot.() -> Unit) { val todoRepository : InMemoryTodoRepository = ... todoRepository.clear() prefilledTodoItems.forEach { todoRepository.add(it) } } } 99 -
fun given(configBlock: Config.() -> Unit) { val config = Config(activityRule)
configBlock(config) } class Config { var prefilledTodoItems = emptyList<TodoItem>() fun todoList(block: TodoListRobot.() -> Unit) { val todoRepository : InMemoryTodoRepository = ... todoRepository.clear() prefilledTodoItems.forEach { todoRepository.add(it) } val robot = TodoListRobot() block(robot) } } 100 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions InMemoryTodoRepository TodoListRobot Config 101 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertContentState + itemToAdd } } 102 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertContentState + itemToAdd } } 103 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions InMemoryTodoRepository TodoListRobot Config 106 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions InMemoryTodoRepository ListenableTodoListViewBinder TodoListRobot Config 107 -
class ListenableTodoListViewBinder(root: View) : TodoListViewBinder(root) { lateinit var stateChangedListener: (State)
-> Unit override fun render(state: TodoListStateMachine.State) { super.render(state) stateChangedListener(state) } } 108 -
class TodoListRobot { init { val viewBinder : ListenableTodoListViewBinder =
... viewBinder.stateChangedListener = { state -> ... } } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 110 -
class TodoListRobot { private val stateHistory = List<State>() init {
val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.add(state) } } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 111 -
class TodoListRobot { private val stateHistory = List<State>() init {
val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.add(state) } } fun assertStates(vararg states: State){ } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 112 -
class TodoListRobot { private val stateHistory = List<State>() init {
val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.add(state) } } fun assertStates(vararg states: State){ Assert.equals(stateHistory, states.toList()) } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 113 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertContentState + itemToAdd } } 114 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertStates(State.Loading) assertStates(State.Loading, State.Content(emptyList()) clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertStates(State.Loading, State.Content(emptyList()), State.Content(itemToAdd)) } } 115 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertStates(State.Loading) assertStates(State.Loading, State.Content(emptyList()) clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertStates(State.Loading, State.Content(emptyList()), State.Content(itemToAdd)) } } 116 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertStates(State.Loading) assertStates(State.Loading, State.Content(emptyList()) clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertStates(State.Loading, State.Content(emptyList()), State.Content(itemToAdd)) } } 117 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "A", false)
given { prefilledTodoItems = emptyList() todoList { assertStates(State.Loading) assertStates(State.Loading, State.Content(emptyList()) clickCreateTodoItem() createItem { enterTitle("A") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertStates(State.Loading, State.Content(emptyList()), State.Content(itemToAdd)) } } 118 -
class TodoListRobot { private val stateHistory = List<State>() init {
val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.add(state) } } fun assertStates(vararg states: State){ Assert.equals(stateHistory, states.toList()) } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 119 -
class TodoListRobot { private val stateHistory = List<State>() init {
val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.add(state) } } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 120 -
class TodoListRobot { private val stateHistory = ReplayRelay.create<State>() init {
val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.accept(state) } } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 121 -
class TodoListRobot { private val stateHistory = ReplayRelay.create<State>() private val
stateVerifier = StateVerifier(stateHistory) init { val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.accept(state) } } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 122 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState } } 125 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState val actualStates : List<S> = stateObservable } } 126 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState val actualStates : List<S> = stateObservable .take(alreadyVerifiedStates.size + 1) } } 127 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState val actualStates : List<S> = stateObservable .take(alreadyVerifiedStates.size + 1) .timeout(10, TimeUnit.SECONDS) } } 128 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState val actualStates : List<S> = stateObservable .take(alreadyVerifiedStates.size + 1) .timeout(10, TimeUnit.SECONDS) .toList() } } 129 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState val actualStates : List<S> = stateObservable .take(alreadyVerifiedStates.size + 1) .timeout(10, TimeUnit.SECONDS) .toList() .blockingGet() } } 130 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState val actualStates : List<S> = stateObservable .take(alreadyVerifiedStates.size + 1) .timeout(10, TimeUnit.SECONDS) .toList() .blockingGet() Assert.assertEquals(expectedStates, actualStates) } } 131 -
private class StateVerifier<S>(private val stateObservable: ReplayRelay<S>) { private var alreadyVerifiedStates:
List<S> = emptyList() @Synchronized fun assertNextState(nextExpectedState: S) { val expectedStates : List<S> = alreadyVerifiedStates + nextExpectedState val actualStates : List<S> = stateObservable .take(alreadyVerifiedStates.size + 1) .timeout(10, TimeUnit.SECONDS) .toList() .blockingGet() Assert.assertEquals(expectedStates, actualStates) alreadyVerifiedStates = actualStates } } 132 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions InMemoryTodoRepository ListenableTodoListViewBinder TodoListRobot Config 133 -
Data Access Layer TodoRepository Application Layer / Business Logic Presentation
Layer TodoListStateMachine TodoListViewModel TodoListFragment TodoListViewBinder pure functions InMemoryTodoRepository ListenableTodoListViewBinder TodoListRobot StateVerifier Config 134 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "A", false)
given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("A") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertContentState + itemToAdd } } 135 -
class TodoListRobot { private val stateHistory = ReplayRelay.create<State>() private val
stateVerifier = StateVerifier(stateHistory) init { val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.accept(state) } } fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 136 -
class TodoListRobot { private val stateHistory = ReplayRelay.create<State>() private val
stateVerifier = StateVerifier(stateHistory) init { val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.accept(state) } } val assertLoadingState: Unit get() = stateVerifier.assertNextState(TodoListStateMachine.State.Loading) fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 137 -
class TodoListRobot { private val stateHistory = ReplayRelay.create<State>() private val
stateVerifier = StateVerifier(stateHistory) init { val viewBinder : ListenableTodoListViewBinder = ... viewBinder.stateChangedListener = { state -> stateHistory.accept(state) } } val assertLoadingState: Unit get() = stateVerifier.assertNextState(TodoListStateMachine.State.Loading) val assertContentStateWithEmptyList: Unit get() = stateVerifier.assertNextState(TodoListStateMachine.State.Content(emptyList())) fun clickCreateTodoItem() { Espresso.onView(ViewMatchers.withId(R.id.newItem)) .perform(ViewActions.click()) } } 138 -
@Test fun newTodoItemIsShownInTodoList() { val itemToAdd = TodoItem("1", "Another Item",
false) given { prefilledTodoItems = emptyList() todoList { assertLoadingState assertContentStateWithEmptyList clickCreateTodoItem() createItem { enterTitle("Another Item") assertEnterTitleState pressNext() assertSummaryState pressSave() assertSavingInProgressState assertSavingSuccessfulState } assertContentState + itemToAdd } } 139 -
class ListenableTodoListViewBinder(private val root: View) : TodoListViewBinder(root) { lateinit var
stateChangedListener: (State) -> Unit override fun render(state: TodoListStateMachine.State) { super.render(state) stateChangedListener(state) } } 141 -
class ListenableTodoListViewBinder(private val root: View) : TodoListViewBinder(root) { lateinit var
stateChangedListener: (State) -> Unit override fun render(state: TodoListStateMachine.State) { super.render(state) stateChangedListener(state) Screenshot.snap(root).record() } } http://facebook.github.io/screenshot-tests-for-android/ 142 -
Localization QA - Freeletics App is localized (9 languages) -
Are all translations correct? - Use same Robots just without assertions - Navigate through our App - Use custom ViewBinder to create screenshots 143 -
Summary - Robot Pattern is very readable - Adding configuration
and assertions → reads like specifications - Test as much production code as possible - State based Architecture helps - Robots could be used without real UI (jvm tests) 145