11

I downloaded Robolectric deckard-gradle project from https://github.com/robolectric/deckard-gradle and imported to Android Studio.

On my first run i got

!!! 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)

Error and i fixed this from .iml.

Then i got:

Class Not Found "my test class"

I tried dozens of solutions which i found in google about this problem but none of them worked.

7
  • Did you get anywhere with your problem? I am in the same boat! :(
    – nemo
    Aug 18, 2014 at 0:22
  • @nemo sorry i couldn't find any solution. i think using latest version of android studio is causing this problem. I gave up searching a solution because i send lots of mail to robolectric team and open topics in robolectric group in google groups but still no solution, no help... Aug 18, 2014 at 6:40
  • 2
    oh I think I fixed it. Add this line to your iml file: <output-test url="file://$MODULE_DIR$/build/test-classes" />. It should go under the <output url=.... element in the iml file. Also make sure that the path for your test-output corresponds to the path where the actual classes are created. Not to mention that you need to add a test gradle task to your AS so that your tests are compiled. See this link for more info and you should be good to go. Hope that helps!
    – nemo
    Aug 18, 2014 at 6:56
  • i tried this solution but it didn't work for me. i hope it works for you. i don't have much time to try a new solution for testing. maybe later i can try this again. thanks for your help. Aug 18, 2014 at 6:58
  • Not really a solution to your problem but you could run the tests from the command line. Aug 29, 2014 at 13:22

5 Answers 5

5

UPDATE: Android Studio 1.1.0 has added JUNIT 4 testing support to the IDE. See more under: https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support. That should fix the STUB exception.

Option one of Alex is the right answer: I use it in two projects now and it is sad you have to do it this way but that's all you can do right now.

The detailed steps are:

  1. when the STUB EXCEPTION is shown copy the first grey line completely into an editor.
  2. In this line, delete everything before -classpath and everything after the last .jar entry. (Like Alex said, the classpath, but also delete the rest after it)
  3. Search for junit and move the line similar to the following one /path/in/filesystem/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar: to the beginning of the dependencies directly after the "-classpath" directive.
  4. Add your test classes output dir to the end of the classpath like -classpath "/dependency1:/dependency2:/...:/Users/user/path/to/project/app/build/test-classes"
  5. Go to "Run Configurations" in the drop down menu where to start the tests or the app
  6. In your junit test configuration (which you tried to run before, not Android Tests) add the text from above as VM options.
  7. Press ok, run the test and voilà, it works!

By the way, for generating the output test-sources I had to add

apply plugin: 'idea'
idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}

to the module's build.gradle

If you still get errors when trying to use the ui, go to the terminal included in android studio and use ./gradle clean check (or an equivalent command) which runs lint checks for your project and the tests. If anything goes wrong with your tests you'll be informed there and have the possibility to view the results on a html-page.

4
  • hmm, after this I have class not found exception for my test class
    – marcinm
    Dec 3, 2014 at 6:16
  • I forgot to say that you also have to add your test-classes output dir, see my updated answer Dec 4, 2014 at 9:01
  • @Andev What should I do if I don't have a test-classes directory under build even though I have configured the apply plugin idea etc.?
    – Niklas
    Dec 5, 2014 at 18:49
  • @Andev Yep I've looked into both
    – Niklas
    Dec 9, 2014 at 10:08
2

I was struggling with the same problem and finally got it to work with this solution (for Android Studio 1.0.2 and Robolectric 2.4):

  • Go to your module's *.iml and move the order-entry with jdkType='Android SDK' to the bottom of all other order-entry-elements (this fixes the "Stub"-issue).

  • Also in the *.iml file add the following output-test entry directly beneath the output-element. It should look like this:

<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/test-classes" />
  • Finally, go to your run configurations Android Studio Menu -> Run -> Edit Configurations
    • Add a new Gradle configuration (Click '+' -> choose 'Gradle'), name it 'Compile test classes' (or something similar), choose your module as Gradle project (just use the icon next to the input field for a quick selection) and fill Tasks with 'compileDebugTestJava', click 'Apply'
    • Now find your JUnit run configuration and find the section 'Before launch: Make' at the bottom. Click '+' and choose 'Run another configuration'. Select 'Compile test classes' (or the name you have set for the Gradle task) and last but not least move that entry before the 'Make' entry which should already be present.

This works perfect for me. Now I am able to run tests directly from Android Studio and don't have to manipulate the class path. This is why I feel this is more convenient.

4
  • I don't know about other people, but that did not work for me :( My Robolectric test class is still not found Jan 23, 2015 at 14:53
  • Marcel, have you setup your build.grade like described here [1]. When you then execute './gradlew clean test' in the Android Studio Terminal, does it generate the folder 'test-classes' in your app/build subfolder of your project? I want to make sure if your issue is caused by trying to run your tests from the IDE or if it's a general configuration issue with Gradle and Robolectric. [1] github.com/robolectric/robolectric-gradle-plugin Jan 26, 2015 at 8:23
  • yes I started from zero and now I'm working :) the ./gradlew clean test is so slow though :( Jan 26, 2015 at 11:29
  • Ok, your configuration is correct then. Go ahead and delete the test-classes folder. Try to launch from IDE and let's see if it's generated again. Make sure to add the <output-test> tag to your *.iml and also try to use the compileTestDebugJava task (note: Test<->Debug has changed compare to the task mentioned in the post above) . Jan 26, 2015 at 12:10
1

The problem is that Android Studio rewrites your .iml file very often so even though you modified it, it probably got changed back.

Option 1: Hack it for Android Studio

What you could do instead is to set the classpath as a VM option on your test run and put the path to JUnit 4 in front of everything else.

In your test run configuration, for VM options add -classpath then the path to JUnit, then the rest of your classpath (you can grab this from the Android Studio output when you try to run the test and it fails, it's on the first line of the output where you copied your message from).

For example:

-classpath "/Users/anothem/.gradle/caches/artifacts-26/filestore/junit/junit/4.11/jar/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar:[the rest of your classpath]

Option 2: Use IntelliJ

This is not really a fix, but I thought I'd mention it. If you don't really want to hang on to Android Studio, you could use IntelliJ instead (Android Studio's "dad").

With IntelliJ, you can open the Project Settings and just reorder your packages so that JUnit4 and Robolectric come in front of everything else.

1
  • Thanks for your answer. I tried your first solution but result still same and i'm working in a company so i don't have a chance to use intellij. Aug 7, 2014 at 6:34
0

So, there are few things to be kept in mind to get over Android Studio issues, few of them could be recommended practices though.

  1. Make sure you are working with JDK 1.7, which is recommended so far for Android development. [Correct your setup]
  2. Current Robolectric-gradle plugin version 1.0.1 is compatible with android-gradle version 1.1.0 [Check your build.gradle file]
  3. Place your tests in "src/test" directory, and specify that in APP build.gradle (inside 'android'), as follows:

sourceSets { testLocal { setRoot('src/test') } }

  1. Right click "src/test/java" folder in "Project" Tab, and click "Mark Directory As">"Test Sources Root"

Just to add, I am using IDEA community edition and it works fine. Also, I am not required to hack .iml file or dig into various other settings inside the IDE.

0

The comment from nemo point it already. You can modify your iml file. (bad that this file is often rewritten from android studio) I use a custom gradle Task to enable unit testing inside android studio. Modify the iml file and change the path to the expected one for test runs. Sometimes i must run my test twice to get it run, because android studio take the changes only at next test run, not the current.

here is a full example which can be included in your gradle file https://github.com/nenick/android-gradle-template/blob/master/Scripts/android-studio-robolectric-support.gradle

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.