Over a million developers have joined DZone.

Approaching Outside-in TDD on Android: Part III

In this post, we will finish implementing a outside-in TDD system and will summarize all that we have learned during the process.

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

In the previous post, we wrote the acceptance test as a first step and started creating the classes on the entry points of our system. In this post, we will finish implementing the system and will summarize what we have learned during the process.

Inner Loop Continuation

To finish the BankAccount class, we need to implement its last public method, showStatement. Let’s dive into the next iteration of the inner loop cycle.

  • Red: we created a `StatementFormatter` to format the statement lines. We considered the statement to be a domain concept important enough to have its own class. It acts as a first class collection around the statement lines that attracts behavior related to the statement lines. The failing test ensures that when we show an account statement, the view is called with the appropriate statement lines.
  • Green: the implementation is quite simple in this case. The BankAccount just needs to create a new statement with the transactions from the repository, pass it to the formatter to format and show the formatted statement using the view.
  • Refactor: nothing to refactor here.

As a rule of thumb, once a class is done, the next step is to run the acceptance test to check the progress and to know which collaborator to implement next.

If we executed the acceptance test at this point, we would see that the TransactionRepository throws an UnsupportedOperationException. As we stated before, throwing an exception in the methods that we have not implemented yet will guide us through the feature implementation and will point us to the next collaborator to implement. It is quite important, as doing so, we have greater control over the current feature progress. We just need to follow the exceptions until the feature is fully implemented.

  • Red: `TransactionRepository` is a leaf node in the tree of collaborators. That means it does not collaborate with other classes. Leaf nodes can not be tested using mocks as there is no collaboration, so we need to flip to the classicist style of TDD and test through state rather than through behavior. TransactionRepository is going to be implemented as an in-memory storage, so we decided to test it by storing a transaction and checking that the list returned in the transactions method contains the previously stored transaction.
  • Green: to make the test pass, we just need to return the current list of transactions. As a best practice, we recommend returning an immutable list. This protects the result from being modified by clients.
  • Refactor: some readability improvements in the test for TransactionRepository. Remember that readability is important both in production and test code.

Now that TransactionRepository is done, if we execute the acceptance test, StatementFormatter throws an UnsupportedOperationException. That means that it should be the next one to be implemented.

  • RedStatementFormatter is responsible for creating ViewStatementLines from a given Statement and sorting them in reverse chronological order. In order to do so, the StatementFormatter has to take the StatementLines from the Statement, map them to ViewStatementLines, and sort them. This is tested by stubbing the StatementLines that the Statement returns and asserting that the output of the format method contains the expected ViewStatementLine information and order.

    Note that we are not going to fulfill the statementLines method yet. Instead, we mock it and throw an UnsupportedOperationException accordingly. We will jump there when needed.

  • Green: as described before, the production code for the StatementFormatter takes the lines from the statement, sort them in inverse chronological order, and map them to ViewStatementLines.
  • Refactor: we extracted a method that maps a line in the production code and did some clean up in the test code.

Once again, we run the acceptance test to check the progress and now it guides us to the lines method in the Statement class (the one that we just mocked in the previous inner loop to implement the StatementFormatter). Let’s get rid of the exception and jump into the next inner loop.

  • Red: statement has to map every transaction to a StatementLine that contains amount, date, and running balance. As in the case of the TransactionRepository, Statement is a leaf node. Therefore, it is tested using the classicist approach by asserting the state of the StatementLines returned in the lines method.
  • Green: the implementation just maps all transactions of the Statement to StatementLines, calculating their current balance.
  • Refactor: we extracted some methods in the production code and improved test readability.

We are now done with the Statement class. Let’s run the acceptance test again to find out that the next class that needs to be implemented is the show method of the ShowStatementActivity.

  • Red: during most of the implementation, we were not dealing with any Android code. At this level we moved back to the to UI layer and, as the code is running inside an Activity, we have to write the unit test using Espresso. Here we need to test that once the show method is called, the RecyclerView contains a list of rows representing the information contained in the given ViewStatementLines.
  • Green: in order to show the required rows in the RecyclerView we need to make some steps. First, we need to create a RecyclerView.Adapter that holds the dataset and creates the required ViewHolder associated with every row contained in the RecyclerView.
  • Refactor: improves test readability.

If we execute the acceptance test at this point, we can see that it passes. We can consider the feature DONE as it passes the given acceptance criteria in an automated fashion.

Conclusions

To wrap up the series, we are going to summarize what we have learned and make some comments about our implementation.

We would like to mention that we have used Espresso to assert the state of the views in Android, but you could use the testing framework that works best for you, i.e., Cucumber.

That being said, we have found that outside-in is an approach that requires good design skills and that we need to have a good idea of how the design of the system will look like beforehand. It usually means that we have built a similar feature or system in the past and therefore we have an overall idea of what we need and where we are led to.

Pros

  • The public interface of every class is always designed to serve an existing client. As we work from the outside in, clients are implemented first, and they define the public API of the classes they collaborate with. The resulting design should read better, adhere well to the “tell don't ask” principle, and tend to avoid feature envy and anemic domain models.
  • Benefits of acceptance tests included in the outside-in flow:
    • Automated requirements validation.
    • Offer a way to check the progress of a feature at any given time.
    • Offer a way to validate our system in an end-to-end fashion.
    • Are written in a non-technical language, therefore can be understood by the business.
  • The addition of acceptance and unit tests offers a complete validation that our system works and fulfills the requirements as expected.
  • Outside-in offers a common workflow for developers.

Cons

  • Because outside-in focuses on how the different parts collaborate, the implementation details of the design are part of the tests. As a result, the tests are coupled to the implementation. This situation introduces a bigger risk to refactoring as the tests have to change when the design of the system changes.
  • Features are guided by acceptance tests. That is fine, but remember that acceptance tests tend to be slower than a unit test and will make the test suite slower as we add more features. It is a trade-off that we need to consider. A possible approach is to include acceptance tests only for the features that are critical to the business.
  • It is usually harder to write behavior test than state tests.

These series have now come to an end with this third post. We hope you enjoyed and learned something.

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Topics:
android ,tdd ,outside-in

Published at DZone with permission of Carlos Morera, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}