Tests give you confidence

Marko Novakovic
2 min readMay 8, 2020

Hello. Let’s talk about TESTING! Yeeey :D. More specifically how tests give you confidence and allow you to code faster with no fear of breaking things and introducing new bugs.

I will tell you short story with no happy ending :(.
Recently I have joined an Android project where group of developers developed app in record time. Code is bad and there are NO tests, not single one. App served the purpose but it has to be maintained and new features should be added. Everything is being done on the MainThread and the time came when that should be fixed, client noted that app is slow. RxJava was introduced to the project and everything was wrapped inside

Single.fromCallable {}

This may not be a problem when we expect that Single to produce some value. Problem are operations we don’t need result from are wrapped inside Single.
Subscribing to mentioned Singles looked like this:

And it worked. For a while…. No tests but at this point you don’t need them (so you may think). It worked until we decided to do the right thing and slowly migrate app to use RxJava correctly. Chains starting at right place, taking care of subscriptions, subscribing at right places etc.

After refactoring UserRepository functions return Completable instead of Unit. What do you think will happen now? Nothing! Operations will NOT run. User will not purchase credits and changes will NOT be saved. Completable should subscribed to in order for operations to complete. Code still compiles and runs btw.

We don’t have unit tests to tell us that code is failing to produce wanted results. In unit tests we would expect values to be there and if values are not present we know we have a problem and we would look in the code for the solution. Problem is that it’s hard to manually track those changes and test them by running app on the phone/emulator. If we don’t test everything our users will get faulty app. Users will complain and it would take long time to find bugs.

Ideally you start you project with TDD approach. Everything is covered with tests, after every change you run tests and see if everything still works. Tests take time to write and it may appear it’s slowing you down. Take our example as an example. Do you think refactoring would take less time if we had tests? Hunting for bugs can be hell. Tests give you speed.

I hope this gave you some perspective and you see some value in writing tests. Hopefully you will start writing tests in your new app or in new modules in existing apps.

--

--