This article describes old stuff. The support for testing in Android Studio is now good enough and this stuff probably doesn't work anymore

Been playing a lot with testing lately, but one thing that bothered me a lot was the shitty hacks one has to do for being able to run Robolectric tests within Android Studio.

I'm sure most of you have encountered this annoying exception: java.lang.RuntimeException: Stub!
There are some useful posts talking about this in Stack Overflow, but the bottom line is always the same: either you have to edit manually the .iml file or you switch to IntelliJ. Either way, you have to make compromises. And that sucks.

At least, for me, none of that stuff worked. But then I found out about gradle-robojava-plugin. You have great documentation in there too.

I am going to work through an example for a library, though using this in an app would be pretty much the same.

Your project-wide build.gradle file neets the robojava-plugin dependency set up:

buildscript {  
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:1.0.0'
    classpath 'com.kageiit:robojava-plugin:1.1.5'
  }
}

You create a new project (let's call it tdd for example) at the same level of your application or library with only a build.gradle file inside, with these contents (keep in mind that 'library' is the name of the main project in this case, could be 'app' in yours):

evaluationDependsOn(':library')  
ext.androidProject = 'library'  
apply plugin: 'com.kageiit.robojava'  

You have to add also the tdd project to your settings.gradle file:

include 'library', 'tdd'  

Your build.gradle file could look similar to this one (I am using Robolectric and Mockito):

buildscript {  
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:1.0.0'
    classpath 'org.robolectric:robolectric-gradle-plugin:0.14.1'
  }
}

apply plugin: 'com.android.library'  
apply plugin: 'robolectric'

version '1.0.0'  
group 'my.library'

robolectric {  
  include '**/*Test.class'
  exclude '**/espresso/**/*.class'
}

dependencies {  
  repositories {
    mavenCentral()
  }
  // Your necessary 
  androidTestCompile 'junit:junit:4.12'
  androidTestCompile 'org.robolectric:robolectric:2.4'
  androidTestCompile 'org.mockito:mockito-core:1.9.5'
}

android {  
  compileSdkVersion 21
  buildToolsVersion '21.1.2'

  buildTypes {
    defaultConfig {
      minSdkVersion 9
      targetSdkVersion 18
      versionCode 20005
      versionName version
      testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
  }

  sourceSets.androidTest.java.srcDirs += 'build/generated-sources'
}

Your directory structure could look like this:

Directory structure

Finally, for configuring the tests in Android Studio you should add a new JUnit configuration with parameters set up to look like this one from my Smart Location library:

JUnit configuration

Please note that the "Use classpath of module" should point to your stub tdd created project.

And that's about it. Now you can run your tests from within the IDE.

Tests running within Android Studio

Enjoy!

Tags

android , tests , robolectric

About the author
comments powered by Disqus