Fork me on GitHub

Robolectric

Quick Start for Eclipse

Maven

To open the project in Eclipse, make sure you have the M2Eclipse plugin installed, then:

Project Creation


Create a project

New Android Project dialog

Add a source test directory to your project

Create a JAVA project for your tests


Create and configure test Java project

Add dependency on the Android project

Add required directory structure and jars to test project

At the command line:

mkdir -p .../MyProjectTest/lib
cp .../robolectric-X.X.X-jar-with-dependencies.jar .../MyProjectTest/lib

Configure build path

Back in Eclipse

Add JUnit library

Add Robolectric jar

Add Android Jars

Create a test Run Configuration


Your tests will not run without this step. Your resources will not be found.

Verify your setup


package com.example;

import com.example.MyActivity;
import com.example.R;
import com.xtremelabs.robolectric.RobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

    @Test
    public void shouldHaveHappySmiles() throws Exception {
        String hello = new MyActivity().getResources().getString(R.string.hello);
        assertThat(hello, equalTo("Hello World, MyActivity!"));
    }
}

To run the tests

If you get a RuntimeException saying: “no such layout layout/main”


It means that you have tried to run a test for which you do not have a Run Configuration set up. To remedy this:

Are these instructions helpful? confusing? Let us know! any feedback is helpful. Thanks -Robolectric