Thanks JetBrains! As of IntelliJ 10.5 makes developing Android applications with Robolectric easy.
This is by far the easiest installation method.
pom.xml.
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.
Use this section for reference if you have issues, or if you need create a project completely from scratch.
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.)
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
cp robolectric-X.X.X-jar-with-dependencies.jar .../MyProject/src/libs/test
cp junit-4.x.x.jar .../MyProject/src/libs/test
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
In the Modules tab of Project Settings
.../MyProject/src/gen.../MyProject/src/gen(you may have to do this several times since IntelliJ automatically replaces this setting from time to time)
.../MyProject/src/test/java folder and click the green “Test Sources” button above the source tree, adding it as a test source folder.../MyProject/src/gen folder and click the blue “Sources” button, adding it as a source folderNOTE: 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.
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.
Select “MyProject” module → “Dependencies tab”
Add the “src” module
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"));
}
}