6

I'm trying to run unit tests with Robolectric in Android Studio. I'm almost there - I see:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
    at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
    at junit.textui.TestRunner.<init>(TestRunner.java:54)
    at junit.textui.TestRunner.<init>(TestRunner.java:48)
    at junit.textui.TestRunner.<init>(TestRunner.java:41)
    at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:185)
    ...

I see also all required dependencies in class path. To fix this error I need to put junit4 dependency before android sdk dependency. Unfortunately I don't see module in Project Structure section and I don't know how to edit iml file to archive this.

Here is screenshot from my project structure: enter image description here

My gradle file:

sourceSets {
   testLocal {
      java.srcDir file('src/test/java')
      resources.srcDir file('src/test/resources')
   }
}

dependencies {
    // compatibility
    compile 'android.compatibility:android-support:v4-r13'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'

    testLocalCompile 'junit:junit:4.11'
    testLocalCompile 'org.robolectric:robolectric:2.2'
    testLocalCompile 'org.easytesting:fest-assert-core:2.0M10'
}

android {
    buildToolsVersion "17"
    compileSdkVersion 18

    productFlavors {
       testLocal{
       }
    }
}

And I selected testLocal flavour in AS.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • 2
    possible duplicate of [IntelliJ IDEA with Junit 4.7 "!!! JUnit version 3.8 or later expected:"](http://stackoverflow.com/questions/2422378/intellij-idea-with-junit-4-7-junit-version-3-8-or-later-expected) – Vic Nov 12 '13 at 10:43
  • Sorry guys, but AS has issue with project structure. I will update my question – Eugen Martynov Nov 12 '13 at 15:19

3 Answers3

4

The problem seems to be due to how Android Studio rewrites `.iml files. According to pivotal/robolectric/deckard-gradle devs:

NOTE: Android Studio aggressively re-writes your dependencies list (your .iml file) and subverts the technique used above to get the Android SDK to the bottom of the classpath. You will get the dreaded Stub! exception every time you re-open the project (and possibly more often). For this reason we currently recommend IntelliJ; we hope this can be solved in the future.

Source: https://github.com/robolectric/deckard-gradle

Apparently you can edit these files by hand but AS will rewrite them each time you sync the project. Alternatively (the deckard devs state), you could use IntelliJ IDEA for a more stable solution.

These is currently also a long discussion on the Robolectric Google group:
https://groups.google.com/forum/#!topic/robolectric/xsOpEwtdTi4

Hopefully the AS team will fix this soon. Running Robolectric and Espresso together would be a huge step forward for testing on Android.

pjco
  • 3,826
  • 25
  • 25
4

With Android Studio 0.5.7 it is possible to keep order of dependencies through "Project Structure" dialog or just putting line androidTestCompile 'junit:junit:4.+' at the top fo dependencies section.

However this is not enough to run tests inside Android Studio. The new error is:

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more

So it looks like my junit dependency is not propagated to junit run. I'm trying to make it working further

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • Looking at the classpath this ends up using, I'm not certain this is actually reordering the dependencies. Instead it looks like it's just removing almost all of them. I think the reason the error is different is because it's failing to even start the tests, so it doesn't get a chance to run into the stub exception. – FuegoFro May 07 '14 at 17:00
  • The question was about UI dependency ordering (with assuming that this will influence on the class path). However UI is showing them correctly now but classpath is not correct. Actually classpath is correct except that it puts android jar on the first place – Eugen Martynov May 07 '14 at 18:51
  • @Eugen Martynov now i am using Android Studio 0.8.0 i have several test case from baseActivity when i run my test case each individually i run's but if run from baseActivity by picking the option all it cases the same error as for u JUnit version 3.8 or later expected: so updated with latest version but it remains the same is there any other go for solution – Manoj Dec 08 '14 at 11:10
-1

Android Studio doesn't have the dependencies screen like IntelliJ does. Until google fixes that I have a workaround I came up with. You can read about it on my blog post here. Hope this helps.

Grim
  • 937
  • 10
  • 24