Reputation: 11921
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.
Upvotes: 11
Views: 5506
Reputation: 7438
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
Upvotes: 0
Reputation: 887
So, there are few things to be kept in mind to get over Android Studio issues, few of them could be recommended practices though.
sourceSets { testLocal { setRoot('src/test') } }
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.
Upvotes: 0
Reputation: 11329
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:
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.
Upvotes: 5
Reputation: 2783
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" />
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.
Upvotes: 2
Reputation: 5151
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.
Upvotes: 1