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. With Robolectric you can write tests like this:
1 2 3 4 5 6 7 8 9 10 11 12
// Test class for MyActivity @RunWith(RobolectricTestRunner.class) publicclassMyActivityTest{ @TestpublicvoidclickingButton_shouldChangeResultsViewText() throws Exception { Activity activity = Robolectric.buildActivity(MyActivity.class).create().get(); Button pressMeButton = (Button) activity.findViewById(R.id.press_me_button); TextView results = (TextView) activity.findViewById(R.id.results_text_view);
sourceSets { test { // Fix for running tests through AndroidStudio // Make sure our resources are on our classpath output.dir(output.resourcesDir, builtBy: "processTestResources") } }
/** * Call this constructor to specify the location of resources and AndroidManifest.xml. * * @throws org.junit.runners.model.InitializationError */ publicApplicationTestRunner(Class<?> testClass)throws InitializationError { super(testClass); }
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. With Robolectric you can write tests like this:
1 2 3 4 5 6 7 8 9 10 11 12
// Test class for MyActivity @RunWith(RobolectricTestRunner.class) publicclassMyActivityTest{ @TestpublicvoidclickingButton_shouldChangeResultsViewText() throws Exception { Activity activity = Robolectric.buildActivity(MyActivity.class).create().get(); Button pressMeButton = (Button) activity.findViewById(R.id.press_me_button); TextView results = (TextView) activity.findViewById(R.id.results_text_view);