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

Author: Ian Darwin
Published? true
FormatLanguage: WikiFormat

Problem:

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.

Solution:

Use Robolectric, the JUnit 4 TestRunner that is fast.

Discussion:

  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.*;

@RunWith(RobolectricTestRunner.class)
public class HistoryActivityTest {

	private HistoryActivity activity;
	private Button listButton;

	@Before
	public void setup() {
		activity = new HistoryActivity();
		activity.onCreate(null);
		listButton = (Button) activity.findViewById(R.id.listButton);
	}

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

	@Test
	public void listButtonShouldLaunchListActivity() throws InterruptedException {
		
		assertNotNull(listButton); 
		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].

No records found.