Android Unit Testing - Best Practice when code references android classes

android, android-testing, junit, unit-testing

Solution

Perhaps have a look at http://robolectric.org/

It mocks out most of the Android SDK so tests can be run in pure Java. This means they can be run much faster in a regular desktop VM.

With that kind of speed, test driven development becomes possible.

Problem

I have a regular JUnit Test Case that tests non-android method logic. The method uses TextUtils for things like TextUtils.isEmpty(). It doesn't make sense to me to make it an AndroidTestCase just to pull in TextUtils class. Is there a better way to instrument this unit test? Like add an android.jar to the test project or something? Similar situation with another test where I want to mock a Context object. I can't mock it without making it extend AndroidTestCase. What is the best practice in these situations where I'm just trying to test non-android logic and don't want it to run on an emulator, yet it touches some android classes? Thank you

Original source