Fork me on GitHub

Robolectric

Quick Start for IntelliJ

Thanks JetBrains! As of IntelliJ 10.5 makes developing Android applications with Robolectric easy.

Maven

This is by far the easiest installation method.

  1. Create a maven based android project
  2. Open IntelliJ
  3. Select File → Open Project… → select pom.xml.

Git Submodules

Android IntelliJ Starter is an Android project generator which configures Robolectric as a git submodule. This is especially useful if you plan on forking Robolectric.


Manual Creation

Use this section for reference if you have issues, or if you need create a project completely from scratch.

Project Creation

Create a project

Fill in the source directory

Select the SDK
(You may need to run the Android tool to download/install an sdk version. Robolectric REQUIRES a Google Apis version of the sdk.)

Prepare directory structures

At the command line:

mkdir -p .../MyProject/src/libs/test
mkdir -p .../MyProject/src/libs/main    #production jars go here e.g. roboguice
mkdir -p .../MyProject/src/test/java
mkdir -p .../MyProject/src/gen

Install downloaded jars

cp robolectric-X.X.X-jar-with-dependencies.jar .../MyProject/src/libs/test
cp junit-4.x.x.jar .../MyProject/src/libs/test

Configure the IntelliJ project

Open the Modules tab of Project Settings

Create a new module

Name: src
Content root: …/MyProject/src # default value
Module file location: …/MyProject/src # default value
Type: java # default selection
Next

No additional facets/technologies required

Configure generated source directories


In the Modules tab of Project Settings

Remove unused source directories from the main project


(you may have to do this several times since IntelliJ automatically replaces this setting from time to time)

Set up source directories for the “src” module


NOTE: you may get an error dialog here reading:
“Cannot save settings Module ‘MyProject’ must not contain source root …/MyProject/src/main/java. The root already belongs to module ‘src’”
To fix this problem follow the steps under “Removed unused source directories from the main project” above.

Set up dependencies for the “src” module


Add the Robolectric jar

Add the JUnit jar -“Add…” → “Single Entry Module Library”

Add the Android libraries

NOTE: Android X.X Google Apis MUST be moved below the junit and robolectric jar.

Set up dependencies for the main Android Project module


Select “MyProject” module → “Dependencies tab”

Add the “src” module

Set up exclusions for the main Android Project module


Verify your setup


In Project View, right click on MyProject>src>test>java → New → Java class → MyActivityTest Add the following source:

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 appName = new MyActivity().getResources().getString(R.string.app_name);
        assertThat(appName, equalTo("MyActivity"));
    }
}