2

I've spent several hours trying to make Robolectric in Android Studio, with no success, and I'm almost up to give it up. I get the following error:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!

Of course I use a junit version newer than 3.8 (4.11, indeed).

I've tried several tricks founds in other questions related to this, such as:

  • move the junit dependency to be the first one as suggested somewhere,
  • tried gradle tricks such as changing the sourceSets.instrumentationtest.setRoot, classpath, dependencies -> instrumentTestCompile, apply plugin: 'android-unit-test', but the Gradle version included in my Android Studio version (0.5.7) doesn't seem to recognize these tags.

I even downloaded examples from GitHub that don't even compile. So I'm getting really tired of this.

Any ideas?

Luis
  • 2,833
  • 3
  • 27
  • 41

2 Answers2

1

This run will fail with a complaint about “JUnit version 3.8 or later expected”. The reason for this is that jUnit 4 is too far down the classpath, and now comes perhaps the least elegant part of this setup: as Kostya Y explains, copy the whole contents of the console with the error message into a text editor and do this:

  1. Remove everything except -classpath “…”
  2. Move jUnit 4 to be the first thing in the classpath
  3. Add an entry for the absolute path to the robolectric test classes directory as the last item in the classpath, in this case /path/to/MyProject/app/build/classes/robolectric.

Now if you try re-running the configuration, you might get an UnsupportedClassVersionError if you’ve previously run the tests in console, or at least this happens to me on my Mac. I don’t know the actual root cause, but if this happens, just do Build > Clean Project to clear out previously built classes.

Source: http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
  • Personally, I decided it was not worth the time and stuck with the supported androidTest that runs on the device. Chances are a newer version of AS/gradle could break these workarounds. Hopefully the issue gets resolved soon: https://code.google.com/p/android/issues/detail?id=65186 – Oleg Vaskevich May 15 '14 at 23:03
1

here is a thread about robolectric + android studio, maybe some answers there will help you Android project with Robolectric and Gradle (Android studio)

Update:

The issue with the classpath modification can be avoided by use latest android studio version and setup idea output paths with gradle idea plugin

apply plugin: 'idea'    
idea {
    module {
        outputDir = file('build/resources/testDebug')
        testOutputDir = file('build/test-classes/debug')
    }
}
Community
  • 1
  • 1
nenick
  • 7,340
  • 3
  • 31
  • 23