Deckard is the simplest possible Android project that uses Robolectric for testing and Maven to build. It has one Activity (with an empty layout), and a Robolectric test that creates that Activity. Deckard also imports seamlessly into IntelliJ, due to IntelliJ's support for Maven. Just import the pom.xml.
This project has been created using Deckard and shows how Robolectric can be used to perform unittesting. Support for Roboguice and Mockito was added in the pom.xml and the example to show the advantage of DI.
The TestGuiceModule was originally written by Moritz Post: http://eclipsesource.com/blogs/2012/09/25/advanced-android-testing-with-roboguice-and-robolectric/.
Note: These instructions assume you have a Java 1.6 JDK installed.
To start a new Android project:
-
Install the Android SDK. On Mac OS X with Homebrew just run:
brew install android-sdk
-
Set your
ANDROID_HOMEenvironment variable to/usr/local/Cellar/android-sdk/<version>. -
Run the Android SDK GUI and install API 18 and any other APIs you might need. You can start the GUI like so:
android
-
Install Maven if you haven't already (run
mvnto check). On OS X (as before) this is easiest with Homebrew (unfortunately we have to install Maven 3.0.x as Maven 3.1.x is currently buggy on OS X):brew install homebrew/versions/maven30
-
Use Maven Android SDK Deployer to maven-ize the Android SDK:
git clone https://github.com/mosabua/maven-android-sdk-deployer.git (cd maven-android-sdk-deployer && mvn install -P 4.3) -
Download the DDA-examples project from GitHub:
wget https://github.com/ddoa/dda-code-examples/archive/master.zip unzip master.zip cp android-deckard-unittest my-new-project
-
In the project directory you should be able to run the tests:
cd my-new-project mvn clean test
-
Optionally, import the project into IntelliJ (or Eclipse) by selecting 'Import Project' in IntelliJ and selecting the project's
pom.xml. When prompted to pick an SDK you just need to select the Android SDK home and your JDK. -
Change the names of things from 'Deckard' to whatever is appropriate for your project. Package name, classes, and the AndroidManifest are good places to start.
-
Build an app. Win.