Robolectric

Robolectric

Robolectric is a testing framework that de-fangs the Android SDK so you can test-drive the development of your Android app.

Robolectric handles inflation of views, resource loading, and lots of other stuff that's implemented in native C code on Android devices. This allows tests to do most things you could do on a real device. It's easy to provide our own implementation for specific SDK methods too, so you could simulate error conditions or real-world sensor behavior, for example.

Usage
@RunWith( RobolectricTestRunner . class) public class MyActivityTest { @Test public void clickingButton_shouldChangeResultsViewText throws Exception { Activity activity  = Robolectric . setupActivity( MyActivity . class); Button pressMeButton  = ( Button ) activity<span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. findViewById( R <span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. id<span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. press_me_button); TextView results <span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);"> = ( TextView ) activity<span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. findViewById( R <span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. id<span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. results_text_view); pressMeButton<span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. performClick; String resultsText <span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);"> = results<span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. getText<span class="pl-k" style="box-sizing:border-box;color:rgb(167,29,93);">. toString; assertThat(resultsText, equalTo(<span class="pl-s" style="box-sizing:border-box;color:rgb(24,54,145);"> " Testing Android Rocks! " )); } }