Approaching Outside-in TDD on Android: Part II
This example walks through acceptance testing and how they fit in the double loop of TDD.
Join the DZone community and get the full member experience.Join For Free
in a previous post , we introduced the bank kata. we explained how we are going to implement it in android, reviewed the different kinds of tests that we use in outside-in and transformed a user story into a bunch of acceptance criteria.
in this post, we will focus more on the practical side. we will show how to build the acceptance test from the acceptance criteria, how acceptance tests and unit tests fit in the double loop of tdd and how we rely on them to guide us through the implementation.
we will be explaining everything following the flow of outside-in tdd in a step by step fashion.
outer loop - acceptance test
as we discussed early on, we have to show the statement lines that compose an account statement. concepts such as statement , statement line and transactions are part of our domain. we will follow them throughout the post and code.
to show the statement lines we decided to use a recyclerview . following the acceptance criteria each statement line has to be formatted as follows:
'date' - 'amount' - 'running balance'
it is worth mentioning that many people leave the ui out of the scope of the acceptance tests. in our case, we consider that in most mobile applications the ui is critical and therefore should generally be included. whether you do it or not is your decision, however, we will show how it can be done. to assert the state of the view we are going to use espresso, the official android ui testing framework. espresso does not offer a way to assert the state of each row in a recyclerview out of the box, so we need to use the following snippet to be able to do so, recyclerviewinteraction . we consider that this piece of code is part of our testing framework. that is why it is already included as part of the initial steps.
as we stated in the previous post, android is responsible for instantiating activities . we can’t instantiate them manually. bearing in mind that dependency injection through the constructor is not an option here, we had to come up with an alternative mechanism to instantiate custom dependencies for tests. for simplicity’s sake, we are not going to use any dependency injection framework. instead, we are going to use the service locator pattern. using this pattern and a static method setinstance we will be able to provide custom dependencies to the activity without using constructor dependency injection.
before asserting that the ui is showing the expected information, we need to define the layout that it is going to use to show it. in android every view has an id. we will need those ids to reference the views in the acceptance test to assert that it is showing the correct information.
having said that, let’s focus on the important stuff.
following the acceptance criteria, defined in the previous post , we have to make two deposits and one withdrawal. notice that withdrawals are transactions with negative amounts.
as outside-in design happens in the red phase , we have to design the skeleton of the classes that we know are needed at this point. we do not need to know and define the whole tree of collaborators, but only the ones that we know that are needed. in contrast with classicist tdd, where everything emerges from the tests, in outside-in we need to do some design up front.
at this point we know we need the following collaborators:
- transactionrepository - we know that we need some storage to save transactions. we decided to follow the repository pattern to hold an in-memory implementation.
- viewstatementline - we are going to use a recyclerview that comes with an adapter. the adapter needs to hold a collection of a type that contains the information required for each row. viewstatementline is that type. it does not have any business logic and it is mapped 1:1 to the information shown in the ui.
- statementformatter - this collaborator will convert the domain objects to the format needed in the ui. date, amounts and text formating is the kind of logic that has to be included here.
- servicelocator - this collaborator provides custom bankaccount instances to the activity.
- clock - time… time has a random factor and is outside of our control. to gain control over it, we have to encapsulate time randomness to have control over it during testing.
we will see the collaborators in more detail later on while we implement them.
once we have finished writing the acceptance test and we see it failing for the right reason (the view is not showing the statement), we will dive into the inner loop and start writing the individual pieces required for the feature.
inner loop - unit test
the first collaborator to implement is the one in the outer layer of the system, in this case, the showstatementactivity .
- c22ab01 - let's start writing a failing test that will lead us to create the ui to show the statement. here we are going to use a vertical recyclerview.
- 8d6122f - once we have the failing test, we have to make it pass by loading the activity layout, holding a reference to the recyclerview and setting a linearlayoutmanager with a vertical orientation.
- 08a593 - we started by including all the code inside the oncreate method. in the refactor phase, we decided to extract an initview method to wrap view initialisation code.
at this point, the recyclerview is initialised and ready to hold the statement rows, but we still need to show the account statement . let’s do it.
- 8520fb - the test has to verify that when the activity is launched, the view gets attached to the account and the bankaccount showstatement method is called. as mentioned before, we do not have control over the activity instantiation so we need to use the servicelocator to provide a test bankaccount for the activity.
- fcb403 - to make the test pass we need to obtain the account reference from the servicelocator, attach the showstatementview (activity) to the account instance, and call its showstatement method.
- bd961c - we added all the code inside the oncreate method. following the same reasoning as in the previous refactor, we extract a initaccount method that wraps bank account initialisation code.
the activity is now complete. the next step would be to implement the next collaborator, the servicelocator . the servicelocator job is to provide bankaccount instances to its clients. let’s implement it doing a second iteration of the inner loop cycle.
- 7fb17b - the test for the servicelocator has to assert that the provided bankaccount is not null.
- 8ce146 - in this case is just as simple as returning a new bankaccunt instance.
- there is no refactor to do in this step, so let's move to the next iteration.
now that the outer layer is done, it is time to move to the next level of abstraction and implement the bankaccount . among the bankaccount public methods we need to decide which to implement first - deposit() , withdraw() and showstatement() . we recommend following the order in which the methods are used in the acceptance test. we will show how we can ensure the order later on. in this case, the method that is used first is deposit() . let’s implement it by going through another iteration of the inner loop cycle.
a1f870 - the behavior of this method is to create a transaction, using the amount parameter and the current time, storing it in some kind of persistence.
remember that the acceptance criteria explicitly states that the statement should be sorted in reverse chronological order and that each line has to contain the transaction date. to be able to test that the transaction was created with the current date we need to stub the clock the same way that was done for the acceptance test. later on, we need to verify that the repository stores the correct transaction.
one last point to make is that the transactionrepository store method is now throwing an unsupportedoperationexception . as we are mocking it in the current test, we do not care what is inside the production code of the transactionrepository. we will explain later on how throwing exceptions is going to guide us showing what we need to implement next.
- 1f5cda - as we have already made the decision about the design, making the test pass is pretty simple. we just need to store a new transaction with the amount parameter and the current time using the transactionrepository. we also needed to implement equals() and hashcode() in the transaction, so that its equality is solved using its fields and not comparing references (default behavior in java).
- there is nothing to refactor here. let's move on and implement the next bankaccount operation.
now it’s time to create the bankaccount withdraw operation.
- 13c1b41 - having a look at the test we can observe that it is precisely the same as the one for the deposit operation, but storing a transaction with a negative amount.
- 83a326 - as defined in the test, the implementation is simply storing a transaction with a negative amount.
- 27da82 - after implementing both deposit and withdraw methods we wondered if the knowledge of those terms belongs to the bankaccount. in our opinion, the bank account should know about the concepts of deposit and withdrawal , but not what defines them internally (a positive or negative amount). therefore, we decided to push the logic related to the amount to the deposit and withdrawal abstractions respectively. deposit and withdrawal are subclasses of the now abstract transaction class. the repository will continue handling transactions. the bankaccount instantiates deposit or withdrawal respectively for the deposit and withdraw operations.
it is important to note that in this case, inheritance is used to specialize the transaction behavior related to the amount and not for code reuse. inheritance is a mechanism aimed for specialization, not for code reuse. the code is now much more expressive, and the responsibilities are where we think they belong.
we have reviewed how to write an acceptance test based on the acceptance criteria. as design occurs upfront, we have decided to create the collaborators that we know that were needed at that time. lastly, we started to explore our system with the subsequent inner loops.
in the next post of the series, we will finish implementing the system. we will conclude summarizing some of the most valuable insights that we have learnt during the process.
Published at DZone with permission of Carlos Morera, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.