Skip to content

Robolectric Installation for Unit Testing

Gautam Jain edited this page May 20, 2015 · 40 revisions

Robolectric is a framework which mocks part of the Android framework contained in the android.jar file and which allows you to run Android tests directly on the JVM with the JUnit 4 framework.

Robolectric is designed to allow you to test Android applications on the JVM. This enables you to run your Android tests in your continuous integration environment without any additional setup and without an emulator running. Because of this Robolectric is really fast in comparison to any other testing approach.

Let's take a look at a step-by-step for setting up Robolectric to test your project.

Setting up for Android Studio

Configure top-level build.gradle

Make sure to be running at least version 1.1.0 of the Android plug-in for Gradle, since unit testing with Android Studio was only recently supported. More information can be found [here] (http://tools.android.com/tech-docs/unit-testing-support).

buildscript {
   repositories {
      mavenCentral()
   } 
   dependencies {
      classpath 'com.android.tools.build:gradle:1.2.2'
   }
}

Setting up this file in the top level will help ensure that there is only one place where this Android plug-in is defined. All other projects (i.e. app) will inherit this dependency.

Configure app/build.gradle

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  testCompile('org.robolectric:robolectric:3.0-rc2') {
         exclude group: 'commons-logging', module: 'commons-logging'
         exclude group: 'org.apache.httpcomponents', module: 'httpclient'
  }
 apply plugin: 'org.robolectric'

Within your dependencies, you need to include the following defines. The exclude modules are intended to remove duplicate dependency definitions (template borrowed from https://github.com/mutexkid/android-studio-robolectric-example).

Creating resource directory

Create an test/resources/org.robolectric.Config.properties file. Note that the directory needs to be resources/ (not to be confused with /res directory used to store your layout files).

# Robolectric doesn't know how to support SDK 19 yet.
emulateSdk=18

Creating tests

  1. See this example. Note that your test needs to be annotated with RoboelectricGradleTestRunner. You also need to annotate the BuildConfig.class, which should be created during a Gradle run.
   RunWith(RobolectricGradleTestRunner.class)
   @Config(constants = BuildConfig.class)
   public class MyActivityTest {

The Roboelectric guide is also a useful resource. Each of your tests must contain an @Test annotation.

  1. If you intend to create mock network responses, you will need to place them inside the src/test/resources/ directory. You can reference these files by using getResourceAsStream() (note the backslash is needed in the front):

       InputStream stream = MyClass.class.getResourceAsStream("/test_data.json");
    

    Note that while running Gradle at the command-line, your tests may pass successfully. However, in Android Studio you may notice that null gets returned whenever trying to make this call. It is documented as a known issue and will be fixed in the next version of Android Studio.

    One workaround inspired from this PasteBin is to add these lines into your app/build.gradle file. What it does it copy all the files from test/resources into the build directory so they can be found during your Android Studio test runs.

       task copyTestResources(type: Copy) {
            from "${projectDir}/src/test/resources"
            into "${buildDir}/intermediates/classes/test/debug"
        }
    
        assembleDebug.dependsOn(copyTestResources)

Running tests

If you type ./gradlew test from the command line, tests should run correctly.

Debugging Tests

If you want to be able to debug your tests inside Android Studio, make sure you are at least using the Android Gradle plug-in v1.1 or above. In older versions, there were problems being able to run these tests directly from the IDE.

  1. Make sure your tests are located in src/test/java.

  2. If you are using a Mac, go to Run -> Edit Configurations -> Defaults -> Junit and make sure to set $MODULE_DIR$ as the working directory. There is a known bug in tests not being located unless you set this configuration first. See the Roboelectric getting started guide for more information.

  1. Select Unit Tests under Build Variants. If you see this dropdown disabled, make sure to verify what Android Gradle plug-in version you are using.
  1. Make sure you run the test as a JUnit test, not as an Android Application Test. You can control-click on the file and click on Run. The icons look different (1st icon is Android test, while the 2nd icon is JUnit.)

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally