I am using Android Studio/IntelliJ
to build on an existing Android
project and would like to add some simple JUnit
unit tests. What is the right folder to add such tests on?
The android Gradle
plug-in defines a directory structure with src/main/java
for the main source code and src/instrumentTest/java
for Android
tests.
Trying to add my JUnit tests in instrumentTest didn't work for me. I can run it as an Android
test (that's what that directory seems for) but that's not what I'm looking for - I just want to run a simple JUnit
test.
I tried creating a JUnit run configuration for this Class but that also didn't work - I'm supposing because I'm using a directory that is flagged as Android
Test instead of Source.
If I create a new source folder and marki it as such in Project Structure, this will get wiped next time IntelliJ
refreshes the project configuration from the gradle build files.
What is the more appropriate way of configuring JUnit tests in an gradle-based android project on IntelliJ
? Which directory structure to use for this?
Intro
Note that at the time of writing robolectric 2.4 is the latest version and has no support for
appcompat v7
library. Support wil be added in robolectric 3.0 release (no ETA yet). AlsoActionBar Sherlock
can cause issues with robolectric.To use Robolectric within Android Studio you have 2 options:
(Option 1) - Running JUnit tests with Android Studio using a Java module
This technique uses a java module for all your tests with a dependency to your android module and a custom test runner with some magic:
Instructions can be found here: http://blog.blundellapps.com/how-to-run-robolectric-junit-tests-in-android-studio/
Also check the link at the end of that post for running the tests from android studio.
(Option 2) - Running JUnit Tests with Android Studio using robolectric-gradle-plugin
I encountered a few issues setting up junit tests to run from gradle in Android Studio.
This is a very basic sample project for running junit tests from a gradle based project in Android Studio: https://github.com/hanscappelle/android-studio-junit-robolectric This was tested with Android Studio 0.8.14, JUnit 4.10, robolectric gradle plugin 0.13+ and robolectric 2.3
Buildscript (project/build.gradle)
The build script is the build.gradle file in the root of you project. There I had to add robolectric gradle plugin to classpath
Project buildscript (App/build.gradle)
In the build script of your app module use the
robolectric
plugin, addrobolectric
config and addandroidTestCompile
dependencies.Create JUnit Test Classes
Now put the test classes in the default location (or update gradle config)
And name your tests classes ending with Test (or again update config), extending
junit.framework.TestCase
and annotate test methods with@Test
.Execute Tests
Next execute the tests using gradlew from command line (make it executable using
chmod +x
if needed)Sample output:
Troubleshooting
alternative source directories
Just like you can have your java source files somewhere else you can move your test source files. Just update gradle
sourceSets
config.package org.junit does not exist
You forgot to add the junit test dependency in the app build script
java.lang.RuntimeException: Stub!
You're running this test with the run configurations from Android Studio instead of command line (Terminal tab in Android Studio). To run it from Android Studio you'll have to update the
app.iml
file to have the jdk entry listed at the bottom. See deckard-gradle example for details.Complete error example:
ERROR: JAVA_HOME is set to an invalid directory
See this SO question for a solution. Add the below export to you bash profile:
The complete error log:
Test Class not found
If you want to run your tests from Android Studio Junit Test runner instead you'll have to expand the build.gradle file a little more so that android studio can find your compiled test classes:
from: http://kostyay.name/android-studio-robolectric-gradle-getting-work/
Some more resources
The best articles I found around this are:
This is now supported in Android Studio starting with Android Gradle plugin 1.1.0, check this out:
https://developer.android.com/training/testing/unit-testing/local-unit-tests.html
Sample app with local unit tests on GitHub:
https://github.com/googlesamples/android-testing/tree/master/unittesting/BasicSample
Please see this tutorial from the Android Developers official site. This article also shows how to create mock-ups for your testing.
By the way, you should note that the scope of the dependencies for simple JUnit test should be "testCompile".
Normally, you can't. Welcome to the world of Android, where all tests must run on a device(except Robolectric).
The main reason is that you don't actually have the framework's sources - even if you convince the IDE to run the test locally, you will immediately get a "Stub! Not implemented" exception. "Why?" you might wonder? Because the
android.jar
that the SDK gives you is actually all stubbed out - all the classes and methods are there but they all just throw an exception. It's there to provide an API but not there to give you any actual implementation.There's a wonderful project called Robolectric which implements a lot of the framework just so you can run meaningful tests. Coupled with a good mock framework (e.g., Mockito), it makes your job manageable.
Gradle plugin: https://github.com/robolectric/robolectric-gradle-plugin
For Android Studio 1.2+ setting up a project for JUnit is pretty simple try to follow along this tutorial:
This is the simplest part setting up a project for JUnit:
https://io2015codelabs.appspot.com/codelabs/android-studio-testing#1
Follow along the past link until "Running your tests"
Now if you want to integrate with intrumentation test follow along from here:
https://io2015codelabs.appspot.com/codelabs/android-studio-testing#6
As of Android Studio 1.1, the answer is now simple: http://tools.android.com/tech-docs/unit-testing-support