Android Unit Testing in Robolectric, using Eclipse

I’m in the process of trying to set up Robolectric for unit testing Android applications. There’s lots of good reasons to consider this framework as an alternative to the standard Android instrumentation-based testing approach, but I will leave that to a future post once I’ve had more experience with the framework.

Right off the bat, I had to set up a test project under Eclipse. These are my notes from the project setup. I’ve followed this process successfully on a Mac with 10.6 Snow Leopard and a PC with Ubuntu 10.10 Maverick Meerkat, both using Eclipse Helios.

1) Create a separate project for testing purposes. Do not create an Android project, or an Android JUnit project. Instead, create a plain old Java Project. (Right click on ‘Package Explorer’, ‘New’ > ‘Java Project’).

2) Name your Robolectric test project something nice, that parallels the main project you are trying to test. Under ‘Project layout’, I chose the default ‘Create separate folders for sources and class files’.

3) Hit Next, to get to the Java build settings.

  • On the ‘Source’ tab, the default ‘src’ folder is where your test cases will go.
  • On the ‘Projects’ tab, add your main Android project as a required project on the build path.
  • On the ‘Libraries’ tab, you will need to add the Android jars, JUnit jars, and the Robolectric jar. Copy the following jar files to the libs folder of your new project, and add them to this tab.  In my case, I created the project first, then copied the jars into the libs folder, then returned to this tab in Project settings to add the dependencies.
    • android.jar (from the android sdk root/platforms/android-8 folder)
    • maps.jar (from the android sdk root/add-ons/addon_google_apis_google_inc_8/libs folder)
    • robolectric-all.jar (downloaded from the Robolectric github page)
    • junit-4.8.2.jar (downloaded from the junit.org website)

4) Hit ‘Finish’.  Your project will be created.

5) At this point, move into src, then create a test package, and a test case class.  Here’s one to get started.

Note the secret sauce on line 11, which runs the test with the Robolectric test runner.  The Robolectric test runner performs all of the behind-the-scenes bytecode manipulation to provide its own implementation for Android APIs at runtime.

You’ll need to import additional classes from Robolectric packages (com.xtremelabs.robolectriccom.xtremelabs.robolectric.shadows) as you move on:

package com.myorg.myapp.test;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.xtremelabs.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class FoobarTestCase {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSomethingMeaningfulToMyApp() {
		fail("not implemented");
	}

}

6) Set up Run Configurations for your test Project:

a) From the Run menu, choose ‘Run Configurations.’  Create a new JUnit test configuration.  Do not create an ‘Android JUnit Test’. Name your configuration, choose JUnit 4 for the test runner.  Make sure ‘Run all tests in the selected project, package or source folder’ is checked, and choose the name of your test project.

b) At the bottom of the dialog, there may be a prompt, “Multiple launchers available — Select one…”.  Click the ‘Select other…’ link, check “Use configuration specific settings” and choose ‘Eclipse JUnit Launcher’.  Because Robolectric tests run on the desktop, and not on an Android device or emulation environment, they do not use Android-specific test launchers.

c) You must set the test project’s working directory to the folder containing your main test project. Select the ‘Arguments’ tab. At the bottom under ‘Working Directory’, select the ‘Other’ radio button.  Click the ‘Workspace…’ button and select the Android project that you are running tests against.

d) click ‘Run’ and your configuration will be saved and your tests run.

Special thanks to the Robolectric team at Pivotal for getting me started, and providing some of the details in the Eclipse configuration I’ve outlined here.

This entry was posted in Android, Test driven. Bookmark the permalink. Both comments and trackbacks are currently closed.

3 Comments

  1. ksncho
    Posted November 19, 2010 at 8:11 am | Permalink

    thanks for good article. but I have a problem to make a test project.
    I am trying to make a Robolectric test project for HelloAndroid project which is made by eclipse.
    When I run the test project with JUnit4, test case fails with an error message like “java.lang.RuntimeException: no such layout layout/main”.
    I know there is a such layout.
    I have no idea what to do next.
    help me please..

  2. Posted November 21, 2010 at 1:59 pm | Permalink

    @ksncho,

    I know the folks at Pivotal helped you out on the Robolectric Google group, but I’ve updated the setup notes above with the helpful information they provided, which hopefully have solved your problem.

  3. saikek
    Posted June 8, 2011 at 5:32 am | Permalink

    Thanks very much!
    You’ve made comlicated things very easy.

One Trackback