Eugen Martynov Eugen Martynov - 28 days ago 5x
Android Question

How to run unit tests with Android Studio

I'm using Jake's Android unit tests plugin for gradle:

My build.gradle looks like this:

dependencies {
// analytics

// retrofit

// dagger

// compatibility

// Picasso

// Otto

// Tests
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.2'
testCompile 'org.powermock:powermock-api-mockito:1.5.1'
testCompile 'org.easytesting:fest-assert-core:2.0M10'

Unfortunately I'm not able to run all or specific unit test form Android Studio. I'm getting error:

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(
at com.intellij.rt.execution.application.AppMain.main(
Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
... 3 more

And this is correct because running command line doesn't include my JUnit dependency:

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -ea -Didea.launcher.port=7533 "-Didea.launcher.bin.path=/Applications/Android" -Dfile.encoding=UTF-8 -classpath "/Applications/Android" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 @/private/var/folders/wq/knhztnf105v2_p1t580tj8h80000gp/T/idea_junit701450667388095664.tmp @w@/private/var/folders/wq/knhztnf105v2_p1t580tj8h80000gp/T/idea_working_dirs_junit4927192380605663413.tmp -socket63849

I wonder if anyone was able to run unit tests in Android Studio? And if it is possible how make it?


Update for AS 1.1+, android gradle plugin 1.1+

Finally it is possible without many tricks. Here is example of project that shows how to setup Robolectric test in Android Studio v1.1+ and android gradle plugin v1.1+:

You can find also there possible issue and workarounds. Yes, Robolectric is complex and not officially supported by Google so it still has some issues. But most of the time it works and brings huge value to your project.

I would also encourage you to start using Robolectric v3+. It is almost released and stable enough.

Old answer for AS 0.x and 1.0x and android gradle plugin version below 1.1

I managed to make it with help of friends.

So basically you need to make next changes to run Robolectric unit tests in Android Studio:

  1. Copy your classpath for test (you can find it as first line in "Run" log)
  2. Open run configuration for your unit tests
  3. Change working dir to folder where AndroidManifest.xml is present
  4. Add VM Option -classpath "<path_to_project_folder>/build/test-classes:<path_to_gradle_cache>/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar:<your old classpath>"

As for me the start of new classpath looks like this:



  1. You can run test only for debug variant
  2. Every new test run configuration requires such manual changes. But this is simply copy/paste of two edit fields

I have Android Studio 0.6 version. Here is again part of my build.gradle file:

buildscript {
  repositories {
    maven { url '' }

  dependencies {
    classpath ''
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
  //  classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.1'
    classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.1-SNAPSHOT'
    classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.10.+'

apply plugin: 'android-sdk-manager'
apply plugin: 'android'
apply plugin: 'android-apt'
apply plugin: 'android-test'

repositories {

android {
  compileSdkVersion 19
  buildToolsVersion "19.1.0"

  packagingOptions {
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/ASL2.0'
    exclude 'LICENSE.txt'

  defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "0.9.0"

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7

  sourceSets {
    androidTest.setRoot( 'src/test' )

dependencies {
  // butter knife
  compile 'com.jakewharton:butterknife:5.0.0'
  // dagger
  compile 'com.squareup.dagger:dagger:1.2.1'

  // apt
  apt 'com.squareup.dagger:dagger-compiler:1.+'

  // AS tests
  androidTestCompile 'junit:junit:4.+'
  androidTestCompile( 'org.robolectric:robolectric:2.3' ) {
    exclude group: 'commons-logging'
    exclude group: 'org.apache.httpcomponents'
  androidTestCompile 'com.squareup:fest-android:1.+'
  androidTestCompile 'org.mockito:mockito-all:1.9.+'
  androidTestCompile 'org.easytesting:fest-assert-core:2.0M10'
  androidTestCompile( 'org.skyscreamer:jsonassert:1.2.+' ) {
    exclude group: 'org.json'

  // tests
  testCompile 'junit:junit:4.+'
  testCompile( 'org.robolectric:robolectric:2.3' ) {
    exclude group: 'commons-logging'
    exclude group: 'org.apache.httpcomponents'
  testCompile 'com.squareup:fest-android:1.+'
  testCompile 'org.mockito:mockito-all:1.9.+'
  testCompile 'org.easytesting:fest-assert-core:2.0M10'
  testCompile 'com.squareup.dagger:dagger-compiler:1.+'
  testCompile( 'org.skyscreamer:jsonassert:1.2.+' ) {
    exclude group: 'org.json'