public
Last active

  • Download Gist
RobolectricGradleSetup.md
Markdown

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'
    }
}

3) Open your project build.gradle file.

4) Add Android Test Plugin.

apply plugin: 'android'
apply plugin: 'android-test'

5) Add Sonatype repo to project repositories.

repositories {
    mavenCentral()
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

6) 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.+'
}

7) Create test source folders.

/src/test/java

/src/test/res

8) 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')
    }
}

9) Sync project with Gradle files.

10) Rebuild.

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

12) 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;
    }

}

13) 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);

    }

}

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.