Android Studio + Robolectric + Gradle Class not found Exception

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

During my first run I received

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

The error and I fixed it from .iml.

Then I got:

 Class Not Found "my test class" 

I tried dozens of solutions that I found on google about this problem, but none of them worked.

+10
android tdd robolectric robolectric-gradle-plugin
Jul 18 '14 at
source share
5 answers

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

Option one of Alex is the correct answer: now I use it in two projects, and you are sad that you should do it this way, but all that you can do right now.

Detailed instructions:

  • when STUB EXCEPTION is displayed, the first gray line is completely copied to the editor.
  • On this line, delete everything before -classpath and everything after the last .jar entry. (As Alex said, the path to the classes, but also delete the rest after it)
  • Find junit and move a line similar to the following: /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 immediately after the directive "-classpath".
  • Add test classes to the dir output directory to the end of the class path, for example -classpath "/dependency1:/dependency2:/...:/Users/user/path/to/project/app/build/test-classes"
  • Go to the "Run Configurations" section from the drop-down menu, where to run the tests or application
  • In your junit test configuration (which you tried to run earlier, and not in Android tests) add the text at the top as VM parameters.
  • Click ok, run the test and voilΓ , it works!

By the way, to generate output test sources I had to add

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

to build.gradle module

If you still get errors when trying to use ui, go to the terminal included in android studio and use. / gradle clean check (or equivalent command) that runs lint checks for your project and tests. If something goes wrong with your tests, you will be informed about this and will be able to view the results on the html page.

+5
Oct 29 '14 at 17:12
source share

I struggled 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 * .iml module and move order-entry with jdkType='Android SDK' to the bottom of all other order-entry elements (this fixes the β€œstub”).

  • Also in the * .iml file, add the following output-test entry directly below 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 the launch settings of Android Studio Menu -> Run -> Edit Configurations
    • Add a new Gradle configuration (press "+" β†’ select "Gradle"), name it "Compile Test Classes" (or something similar), select your module as a Gradle project (just use the icon next to the input field for quick selection) and fill in the "Tasks" "compileDebugTestJava", click "Apply"
    • Now find your JUnit startup configuration and find the "Before you Run: Make" section below. Click "+" and select "Run another configuration." Select Compile Test Classes (or the name specified for the Gradle task), and last but not least, move this entry before the Make entry, which should already be present.

This works great for me. Now I can run tests directly from Android Studio and no need to manipulate the class path. That is why I feel it is more convenient.

+2
Jan 09 '15 at 7:33
source share

The problem is that Android Studio overwrites your .iml file very often, so although you modified it, it probably was changed.

Option 1 : hack it for Android Studio

Instead, you should set the class path as a virtual machine parameter in your test run and put the path to JUnit 4 in front of everything else.

In the test run configuration for VM options, add -classpath , then the path to JUnit, and then the rest of your class path (you can extract this from Android Studio output when you try to run the test, and it does not work, it is in the first line of output, where You have copied your message).

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 actually not a problem, but I thought I mentioned it. If you really don't want to hang on Android Studio, you can use IntelliJ instead (Android Studio "dad").

With IntelliJ, you can open the project settings and simply reorder your packages so that JUnit4 and Robolectric come in front of everything else.

+1
Jul 27 '14 at 17:49
source share

A comment from nemo points to this already. You can modify your iml file. (it’s bad that this file is often overwritten from android studio). I am using the gradle custom task to enable unit testing inside android studio. Modify the iml file and change the path to what is expected for test runs. Sometimes I have to run my test twice in order to run it, because android studio only accepts changes at the next test run, and not in the current one.

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

0
Sep 09 '14 at 6:02
source share

So, there are a few things you need to keep in mind in order to overcome problems with Android Studio, however some of them may be recommended.

  • Make sure you are working with JDK 1.7, which is recommended for Android development. [Correct the setting]
  • The current version of Robolectric-gradle version 1.0.1 is compatible with android-gradle version 1.1.0 [Check build.gradle file]
  • Put your tests in the "src / test" directory and specify this in the APP build.gradle (inside the "android") as follows:

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

  1. Right-click the src / test / java folder in the Project tab and click Mark Directory As> Test Source Root

Just to add, I use the IDEA community and it works great. Also, I don’t need to hack the .iml file or dig out other settings inside the IDE.

0
May 01 '15 at 7:02
source share



All Articles