Skip to content

Instantly share code, notes, and snippets.

@MatthewPatience
Last active January 3, 2016 12:09
Show Gist options
  • Save MatthewPatience/8461314 to your computer and use it in GitHub Desktop.
Save MatthewPatience/8461314 to your computer and use it in GitHub Desktop.

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 {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
        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 {
    mavenCentral()
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}
  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.

/src/test/java

/src/test/res

  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 {
        instrumentTest.setRoot('src/test')
    }
}
  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 {
        super(testClass);
    }

    @Override
    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),
                    Fs.fileFromPath(assetsProperty));
        }
        AndroidManifest appManifest = super.getAppManifest(config);
        return appManifest;
    }

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

    @Test
    public void userInterfaceTest() {

        Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
        TextView tvHello = (TextView) activity.findViewById(R.id.tvHello);
        String resultsText = tvHello.getText().toString();
        Assert.assertEquals(
                Robolectric.application.getResources().getString(R.string.hello_world),
                resultsText);

    }

}
  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