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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With