Deckard is the simplest possible Android project that uses Robolectric for testing and Gradle to build. It has one Activity, a single Robolectric test of that Activity, and an Espresso test of that Activity.
Deckard imports easily into the latest editions of Android Studio with minimal setup.
Note: These instructions assume you have a Java 1.8 JDK installed.
To start a new Android project:
-
Install Android Studio 1.1.0.
-
Run the Android SDK Manager and install
API 19
andBuild-tools 21.1.2
. -
Download Deckard from GitHub:
wget https://github.com/robolectric/deckard-gradle/archive/master.zip unzip master.zip mv deckard-gradle-master my-new-project
-
Create a
local.properties
file in the root of the project that points to your SDK installation location. Importing the project into Android Studio (detailed below) will do this for you. -
In the project directory you should be able to run the Robolectric tests:
./gradlew clean test
-
You should also be able to run the Espresso tests:
./gradlew clean connectedAndroidTest
Note: Make sure to start an Emulator or connect a device first so the test has something to connect to.
-
Change the names of things from 'Deckard' to whatever is appropriate for your project. Package name, classes, build.gradle, and the AndroidManifest are good places to start.
-
Build an app. Win.
Deckard gradle is designed to run against Android Studio 1.1.0 with "Unit Testing support" enabled in Studio's Gradle settings.
Import the project into Android Studio by selecting 'Import Project' and selecting the project's build.gradle
. When prompted, you can just pick the default gradle wrapper.
To run Robolectric tests (example can be found in DeckardActivityTest) open Studio's "Build Variants" pane and change the "Test Artifact" to "Unit Tests". You can then run Robolectric tests using the JUnit test runner.
To run Robolectric tests (example can be found in DeckardEspressoTest) open Studio's "Build Variants" pane and change the "Test Artifact" to "Android Instrumentation Tests". You can then run Espresso tests using the Android test runner.