Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio + Robolectric + Gradle Class Not Found Exception

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.

like image 999
savepopulation Avatar asked Jul 18 '14 13:07

savepopulation


2 Answers

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.

like image 135
luckyhandler Avatar answered Oct 16 '22 11:10

luckyhandler


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.

like image 2
André Diermann Avatar answered Oct 16 '22 10:10

André Diermann