[TUT] How to run Robolectric JUnit tests in Android Studio

After my blog post on describing an Android Gradle App with Robolectric JUnit tests. I follow up here with how to run these tests within your AndroidStudio. The outcome being faster feedback from your tests and great IDE integration.

Tests running in ASide

Ok here we’ll explain how to create a run configuration that will run all the tests within your roboelectric tests folder, you can customise this to be selective about what tests to run if you wish so don’t panic. The instructions are very GUI heavy so it’s less talking more screenshots!

Goto your run configurations screen. This is where your run configruations are for building your app, running your tests, deploying your apk. You may not come here often as most of the time they are auto-generated for you.

edit run configurations

Then firstly before we make our test running configuration we need to make another that will be a dependency. This is a gradle task that will refresh the codebase so that your tests will always run against the latest code changes in your app. Thanks to Mark Allison who figured this out when we where getting some strange test failures.

Add gradle run config

You want this config to run your gradle testClasses task like the below, the actual name of your configs is completely personal choice.

gradle build testClasses config

Next you create your JUnit config, now we are getting to it, running your tests!

create JUnit config

This config as we said at the start depends on the gradle config, therefore you need to add that as a dependency. (This + is near the bottom of the screen)

run another config button

Now setup your config:

  1. Ensure dependency added as above.
  2. Give it a name.
  3. Test Kind: All in package, this is so we run all tests you can modify this if you only want to run 1 single or 1 subpackage of tests.
  4. Ensure the working directory is the module, this allows Robolectric to find your AndroidManifest and resources as it uses relative directory lookups.
  5. Use classpath of module: this is the module with the tests inside, ensuring it can find JUnit & other test dependencies
  6. Finally select ‘use alternative jre’ and point this to your Java install on the machine. This is the moneymaker that helps avoid some robolectric & android stub issues.

Robolectic run config

Here is a reminder of the project structure for those who did not come from the last blog post.

Package Structure

Thats it, click OK and you can run your config! When it runs you’ll first see it run the gradle script and then the output of your tests. Woohoo.

running testClasses

runnin Robolectric tests

Also I’d like to thank Xavi Rigau my pairing partner who helped with this config and a lot of gradle things we hack together.

Questions, comments see below.

19 thoughts on “[TUT] How to run Robolectric JUnit tests in Android Studio

    1. package private? If you mean default visibility or package protected visibility, then yes as long as your package name/structure is identical you can test

  1. This is a really excellent set of tutorials. It provides a pragmatic solution to the current (as of 11/14) landscape of android testing.

    I just finished vetting what you guys put together and it’s exactly what I was looking for–thanks.

    *** Note
    With Robolectric 2.3, you no longer need that extra RobolectricGradleTestRunner… instead, you can use the following syntax
    @Config(manifest = “../app/src/main/AndroidManifest.xml”)

    This change requires the Working directory of the Test Configuration to point towards the test module’s folder.

    Really great stuff!!! Cheers & Thank You

  2. Could you explain the significance of running the testClasses task? Is this a built in task or did we define it earlier in the tutorial?

  3. I’m getting an error

    Error:(7, 0) Gradle: A problem occurred evaluating project ‘:robolectrick-tests’.
    > Could not find property ‘android’ on project ‘:app’.

    On this line: testCompile androidModule.android.applicationVariants.toList().first().javaCompile.classpath

    Please help me to solve the problem.

    1. I was getting this error for quite a while. I used the File->Invalidate Caches/Restart option in Android Studio and it went away.

      In 0.8.9 I’m getting an error with the test module finding com.android.support:support-v4:19.0.1 but the tests run just fine if I run them from the command line.

      I believe there are issues with resolving various things in the build.gradle file of my Robolectric module.

      I believe the problem I’m having is with these two lines not resolving correctly for some reason:

      testCompile androidModule.android.applicationVariants.toList().first().javaCompile.classpath
      testCompile androidModule.android.applicationVariants.toList().first().javaCompile.outputs.files

      If you’re having issues try running tests on the command line. You may have more luck.

      1. I’m having these same issues. I can run the tests from the command line, but I’m unable to do so from AS. Would love to have a solution for this!

      2. So things started working for me. I was messing around with multiple things, so I’m not sure which resolved the problem. I still see the support library error, but then my tests kick off and run as expected.

        Things I did around the time it started working…
        – Modify/add my TestRunner,
        – Modified my JRE (actually using Java 8… Retrolambda user) in the build config
        – Set JAVA_HOME path variable(s)… again… Retrolambda thing
        – File->Invalidate Caches/Restart (had to actually close and restart AS 2x)

  4. Depending on how you installed Java, you may need to look in another location for the alternative JRE step:

    /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

    In my case, I used the Oracle supplied .dmg to install Java on Mavericks.

  5. I get all sorts of issues in my project. It’s a fairly complicated codebase with multiple modules as it is. Now when I add this java module Gradle can’t find the Android Support V4 package (version 13.0.0 is needed by one of the other modules) and so it fails to sync.

    Is there something obvious that I’m missing? I’m really looking forward to being able to run tests in a local JVM, and this feels like the last blocker.

  6. I get a

    Class not found: “org.mypackage.tests.MyAndrodTest”

    And, ./gradlew test no longer works.

    Any idea of what might be happening?

  7. Great tutorials (this and the previous set up step)!

    I’ve been resisting the switch from my trusty maven + robolectric combo for projects for a year until the GUI tooling was able to support debugging and running the tests in the IDE.

    The only thing I found confusing about the tutorial was the alternative JRE step. I took some time searching around my system to no avail, so it’d be great to flesh that step out more. I ended up just skipping that step until I run into problems.

Leave a Reply

Your email address will not be published. Required fields are marked *