Last Updated: September 29, 2021
· ravishanker

Testing on Android Studio

After watching Google IO 2013 if you couldn't resist the temptation to try out the new svelte Android Studio. You would have soon hit a snag when it came to testing.

Unlike on Eclipse or ADT Bundle, The new Android Studio doesn't require a separate android testing project.

Good news, It's all integrated now within Studio.

There are usually the following options for testing for Android apps

  • Instrumentation Tests
  • Junit by extending AndroidTestCase (JUnit v3.7)
  • Junit (v3.8 or higher) separately
  • Popular testing frameworks like Robotium or Robolectric

For the first two scenarios you just need to create the following folder structure under src folder

  • src
  • - - - instrumentTest
  • - - - - Java
  • - - - - - com.example.spinner.test
  • - - - - - -

You can then run a particular test file or a package of instrumentation tests. The new build automatically generates required config and AndroidManifest.xml file automatically for you.

However, if you need to use JUnit for POJO testing exclusively or using popular frameworks then there is no integrated support yet in Android Studio. You would have to follow the steps below to configure gradle build system that is used in Android Studio.

In build.gradle file inside src folder create the following blocks

  • a SourceSet
  • a Configuration
  • and Dependencies
  • a test task
sourceSets {
    testLocal {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/resources')

dependencies {
    compile 'org.roboguice:roboguice:2.0'
    compile ''
    testLocalCompile 'junit:junit:4.8.2'
    testLocalCompile 'org.robolectric:robolectric:2.1'
    testLocalCompile ''
    testLocalCompile ''
    testLocalCompile 'org.roboguice:roboguice:2.0'

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir { dir ->
        def buildDir = dir.getAbsolutePath().split('/')
        buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)

    classpath = sourceSets.testLocal.runtimeClasspath

check.dependsOn localTest

It that sounds too much then do not despair there is a plugin in the making for robolectric-plugin

There is more at about the new gradle based build system in Android Studio.

I'm sure there will be better integrated features on the release candidate of Android Studio. Till then enjoy TDD with the sleek intelliJ based Android Studio through these hacks.

2 Responses
Add your response


thank you for sharing! Unfortunately I stuck with AS 0.3.5. It says JUnit 3.8 error and I don't know how to put JUnit 4.0 dependency before Android dependency in running class path. Do you have any ideas?

Kind regards,

over 1 year ago ·

Eugen, it was hard but I think I got it working. I followed these instructions:

over 1 year ago ·