Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Unit Testing with Robolectric and JUnit 4.x

In Chapter: Testing
Author: Ian Darwin ('idarwin')
Published? true
FormatLanguage: WikiFormat


You like unit testing, but Android's test framework is based on the ancient Android 3.8, and it runs the full emulator so it's slow.


Use Robolectric, the JUnit 4 TestRunner that is fast.


  1. Assuming you have your "main" project set up (normal Android project), create a folder called e.g., "test" in this project (do not mark it as as source folder)
  2. Create a separate project, not using the Android Test Project wizard
  3. Make this project depend on your main project (Build Path->Configure)
  4. Remove the default source folder ‘src’ from the new project's build path
  5. Still in Build Path, click “Link additional source”; browse to and select ”…/MainProject/test”
  6. Add Robolectict-2.x.jar to new project's classpath
  7. Add JUnit 4 (not 3.8!) to your new project's classpath, either explicitly, or implicitly by choosing JUnit 4 from the "New Class->JUnit Test" wizard.
  8. Annotate your JUnit 4 tests to run with the RoboLectrict test runner (example below)
  9. Use Robolectric "shadow" classes where needed
  10. Important: create an Eclipse Run Configuration with the following special attributes (this section is quoted from the RoboLectric web site):
  • “Run” → “Run Configurations…”
  • Double click ”JUnit” (not “Android JUnit Test”)
  • Name: MyProjectTestConfiguration
  • Select the “Run all tests in the selected project, package or source folder:” radio button
  • Click the “Search” button
  • Select “MyProjectTest”
  • TestRunner: JUnit 4
  • Click on the link “Multiple launchers available Select one…” at the bottom of the dialog
  • Check the “Use configuration specific settings” box
  • Select “Eclipse JUnit Launcher”
  • Click “OK”
  • Click the “Arguments” tab
  • Under “Working directory:” select the “Other:” radio button
  • Click “Workspace…”
  • Select “MyProject” (not “MyProjectTest”, The value inside of ‘Other’ edit box should be ‘${workspace_loc:MyProject}’)
  • Click “OK”
  • Click “Close”

Here is a sample Robolectric Unit Test.

import static org.junit.Assert.*;

import org.junit.*;
import org.junit.runner.*;

import com.xtremelabs.robolectric.*;
import com.xtremelabs.robolectric.shadows.*;

public class HistoryActivityTest {

	private HistoryActivity activity;
	private Button listButton;

	public void setup() {
		activity = new HistoryActivity();
		listButton = (Button) activity.findViewById(R.id.listButton);

	public void didWeGetTheRightButton() {
		assertEquals("History Log (Morning)", (String) listButton.getText());

	public void listButtonShouldLaunchListActivity() throws InterruptedException {
		boolean clicked = listButton.performClick();
		assertTrue("performClick", clicked);

		ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
		Intent startedIntent = shadowActivity.getNextStartedActivity();
		assertNotNull("shadowActivity.getNextStartedActivity == null?", startedIntent);
		ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);
		assertEquals(WeightListActivity.class.getName(), shadowIntent.getComponent().getClassName());		

See Also:

Robolectric is at [1].


If you found this recipe useful, why not buy the book and have the whole collection always at hand?"