Android – Test Driven Development For Android

androidtddtesting

Can we use JUnit for test driven development in Android ? If not, is there an alternative similar to JUnit ?

I searched a bit on google and also read a SO post Android Test Driven Development
It looks like Android was never made with TDD in mind. I wanted to be sure before I begin learning TDD and doing Android development at the same time.

Best Answer

I think you can completely rely on Robolectric for running your tests in JVM. You get to use JUnit4 for testing your POJOs and Robolectric provides you the support for testing the Android components.

I am also a beginner in TDD for Android Development. I find Robolectric really useful for test driving my code.

This video will tell you almost everything it provides you for unit testing the Android code.

UPDATE: With the Android studio support and the new Android ecosystem, now unit testing can be done as a first class practice. Refer http://developer.android.com/training/testing/unit-testing/local-unit-tests.html for more details.

There are couple of good approaches to test drive the android code. The most effective ones I have found so far is using MVVM(model-view-viewmodel) or MVP(model-view-presenter) approach, where the business logic as well as presentation logic is decoupled from the view and can be easily be unit tested.