Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Set up Robolectric with Gradle

  1. Open your top level build.gradle file.

  2. Add Sonatype repo to buildscript repositories and Square Gradle Android Test Plugin to buildscript dependencies. (Older Gradle projects may automatically put this buildscript code in your project build.gradle, in which situation you should put the following in that file.)

buildscript {
    repositories {
        maven {
            url ''
    dependencies {
        classpath ''
        classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
  1. Open your project build.gradle file.

  2. Add Android Test Plugin.

apply plugin: 'android'
apply plugin: 'android-test'
  1. Add Sonatype repo to project repositories.
repositories {
    maven {
        url ''
  1. Add the following to your project dependancies.
dependencies {
    testCompile 'junit:junit:4.10'
    testCompile 'org.robolectric:robolectric:2.1.+'
    testCompile 'com.squareup:fest-android:1.0.+'
    instrumentTestCompile 'junit:junit:4.10'
    instrumentTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
    instrumentTestCompile 'com.squareup:fest-android:1.0.+'
  1. Create test source folders.



  1. Specify test configuration in android options in the project build.gradle.
android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 13
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        testPackageName "com.bnotions.robolectricgradletest.test"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    sourceSets {
  1. Sync project with Gradle files.

  2. Rebuild.

  3. Create package com.yourcompany.yourproject.test inside the /src/test/java folder.

  4. Add the following class to it.

public class RobolectricGradleTestRunner extends RobolectricTestRunner {

    public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {

    protected AndroidManifest getAppManifest(Config config) {
        String manifestProperty = System.getProperty("android.manifest");
        if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) {
            String resProperty = System.getProperty("android.resources");
            String assetsProperty = System.getProperty("android.assets");
            return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
        AndroidManifest appManifest = super.getAppManifest(config);
        return appManifest;

  1. Create your first test. All test files must specify the following @RunWith annotation on the class.
public class MainActivityTest {

    public void userInterfaceTest() {

        Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
        TextView tvHello = (TextView) activity.findViewById(;
        String resultsText = tvHello.getText().toString();


  1. Run your tests from the command line by calling ./gradlew test

  2. Your test report can be found at /<Project>/build/test-report/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment