The goal of this project is to speed up the setup of Robolectric, Robotuim, Double Espresso, Roboguice, and Mockito (along with some other nice libraries) in Android Studio. Since there is not built in support for Robolectric (just yet, hopefully) there is a lot of manual set up that has to be done. This project uses jeske717's Robolectric-gralde plugin as well as his InjectedTestRunner (more about it later) found on Java Center.
How is this different than the Deckard-Gradle template provided by Robolectric?
Deckard requires you to download their template and then customize things like the name of your project, module name, etc. This template is configured with the information provided when you go through the project creation flow.
- Mac OSX (Mavericks)
- Android Studio 0.8.1, 0.8.5 and 1.0
- Clone the repo
- Go to your Android Studio installation fodler, and then to
- Create a backup of the
NewProjectTemplatefolders (just in case you want to revert the changes)
- Copy the two folders you just cloned into the
- Open Android Studio and create a new project
- After build.gradle gets executed by Android Studio, you would get a dialog saying that the *.iml file was updated and the project needs to be reloaded. Click on OK. (if the dialog doesn't show automatically you should manually run the
addRobolectricTestSourcesToImlGradle task (see below on how to run Gradle tasks)).
- Upon restart the
robolectricTest/javadirectory should be green. Now you need to run the
configureJUnitDefaultToUseRobolectricClasspathGradle task. To do so, go to
View-> Tool Windows->Gradle. Expand the menu that has the name of your app, and double click on the
configureJUnitDefaultToUseRobolectricClasspathtask. Android studio will prompt the same message as before (requesting to reload the project). Click OK.
- After Android Studio reloads, you should syncronize your project.
- Run the
MyActivityRobolectricTest.javainside the robolectricTest module. Right click on the method,
Run>testMethodName()(the second option on the dropdown; the JUnit one).
- Go to Android Studio > Preferences > Compiler > uncheck "Use in-process build"
- As part of the Espresso configuration a custom Run Configuration is needed. They show how to do it on their docs here (look at the Android Studio picture). Make sure you check the "Show chooser dialog". It defaults to Emulator.
- If everything is set up correclty, the test should run and pass.
- Apparently Android Studio 1.0 defaults to
ActionBarActivityregardless of your
minSdkVersionselection. Make sure your
Activityor that you fix the Instrumentation tests to run on older versions of Android.
InjectedTestRunner is the "glue" between Mockito and Roboguice. It basically looks for all
@Mock on the test and automagically(thanks to jeske717) binds the mock instance to the correct
@Inject on the implementation class. For more information go here
Known Limitations (We will try to fix)
- You might encounter some problems when trying to add a new package under
- After you create a new test on a class that already contains tests, sometimes you will need to run the tests twice if you are running all the tests on the class. Synchronizing the project also works. It seems that gralde doesn't pick up the new added test on the first run. It works on the second run. (Step 10 above should fix this)
- Make sure you create the project setting KitKat as
minSdkto begin with. Tried using a different
minSdkduring the project creation flow and it broke the template. You can change your
minSdkafter you follow the steps above.
- Specifying to use the GoogleInstrumentationTestRunner in
build.gradle, as required by Espresso, makes the whole test suite run. Removing
build.gradle, allows to run Robotium tests independently, but Espresso tests will not run. I will try to find a way to separate the runs.
- In order to see the
robolectricTestfolder, you need to switch Android Studio from the
Android Viewto the
- Be able to run Robotium and Espresso tests independently