Skip to content

Instantly share code, notes, and snippets.

View Nilhcem's full-sized avatar

Gautier MECHLING Nilhcem

View GitHub Profile

#[Tutorial] Android unit testing with Gradle, Robolectric, Dagger and Mockito Not long ago I faced the problem of setting up Gradle, Robolectric and Dagger for unit testing my app. And after tinkering a bit, I managed to make them all work together. In this tutorial I assume that you know how to work with Gradle, Robolectric, Dagger and Mockito separately.

I'm just going to show that they can work together pretty neatly, and how can they make Android testing a lot easier ##Wiring Robolectric and Gradle First of all, you're going to need a fully set up Android project ready to be built with Gradle. And I'm assuming that you're using default project layout (e.g. your java sources are located in $projectDir/src/main/java/)

For the next step we need to install a [Gradle Android test plugin][1] to a local maven repository:

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.)