I completely agree with Martin Fowler:
To me, legacy code is simply code without tests. ... Code without tests is bad code. It doesn't matter how well written it is; it doesn't matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don't know if our code is getting better or worse.
Unit testing is a fundamental aspect of computer science that is often ignored. We never talked about it in college and I didn't start hearing much about it until a few years into my coding career.
I am a fairly recent convert to unit testing. At first I didn't see the value. It felt like extra work. A few years ago, I spent three months of intense pairing with another Android developer and it changed my whole outlook! Seeing how much we wrote each day and how much confidence we ended up with in our design was refreshing. It was a departure from other groups I had worked with that only valued quick code. This mentality encourages gun slinging, cowboy coders.
Many languages and frameworks highly value testing, but it's an area that Android overlooked. There are some testing capabilities in Android, but they rely on the emulator or an attached devices and are too slow to use as the primary means of testing during the development phase. This type of testing is better suited for functional and/or integration-based testing of features in your application.
When not using testing, the alternative is to build the application and see what happens, look at app to verify expected behavior, and read logs. It's not terribly efficient!
Unit testing is a best practice, but has been particularly difficult in Android. One library that makes this process much simpler is Robolectric.
Robolectric allows you to focus closely on the application you are building as you build out piece by piece. You write just enough code to pass the tests. It allows you to do targeted and broad refactoring without worrying about breaking the rest of the system. It helps us write better code and helps keep us on track.
Robolectric was created to run core Android components on the JVM and allow developers to verify aspects of their code as they are developing instead of after the feature has been deployed to an emulator or device.
Running tests on an Android emulator or device is slow! Building, deploying, and launching the app often takes a minute or more. That's no way to do TDD. There must be a better way.
Wouldn't it be nice to run your Android tests directly from inside your IDE? Perhaps you've tried, and been thwarted by the dreaded java.lang.RuntimeException: Stub!?
Robolectric is a unit test framework that de-fangs the Android SDK jar so you can test-drive the development of your Android app. Tests run inside the JVM on your workstation in seconds.