At work we are starting to testing our base code using robolectric, but man this thing is thought!

Hopefully robolectic people has a gradle plugin and a example project to see how to use it.

And everything is nice and wonderful while you are launch your test from command line with ./graddle test

But when you go to the real of Android studio, things start falling apart....

Create a junit run configuration

On last step be sure to select the module where the test belong.

Also for not having to create a custom roboletric test runner, set the working directory the folder where the AndroidManifest.xml exist in your file system.

Junit 3.8 Stub - First problem

When executing the junit, that what you get:

After search a while on internet seems that something is wrong with the classpath of junit that intellij expects. The workarround? open the iml file of your project and move the orderEntry of type "jdk" to the last orderEntry.

<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />  
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" exported="" name="android-support-v4" level="project" />
    <orderEntry type="library" exported="" name="support-v4-21.0.0" level="project" />
    <orderEntry type="library" exported="" scope="TEST" name="testrunner-1.1" level="project" />
    <orderEntry type="library" exported="" name="1_testrunner-1.1" level="project" />

to

    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" exported="" name="android-support-v4" level="project" />
    <orderEntry type="library" exported="" name="support-v4-21.0.0" level="project" />
    <orderEntry type="library" exported="" scope="TEST" name="testrunner-1.1" level="project" />
    <orderEntry type="library" exported="" name="1_testrunner-1.1" level="project" />
    <orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />

Nice, at least we have found some workaround and is just moving a line on a file, Well not as easy as it seems, because Android Studio rewrites the iml file a lot of times (surely each time you add remove a dependency)

Test Class not found

Okay, test classes seems to have been not compiled and this is reasonable since nobody told Android studio which is the robolectric task to compile the test files.

So we have to create a new step before executing the junit, that creates the test classes

And finally link to the junit run configuration

Nice that should solve our class not found problem.

Woops, Seems that everything is like before, so first I made sure the ConnectivityTest class exist .

$ find . | grep ConnectivityTest

./samples/Flym/app/build/test-classes/com/example/app/roboelectric/ConnectivityTest$1.class
./samples/Flym/app/build/test-classes/com/example/app/roboelectric/ConnectivityTest.class

Nice, test classes exist then it must be there are not in the classpath when executing the junit.

After surfing for solutions... we found another workaround on stackoverflow and guess what, yeah again modify the iml file.

Just add a <output-test url="file://$MODULE_DIR$/build/test-classes" /> after the output node like that

<component name="NewModuleRootManager" inherit-compiler-output="false">  
    <output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
    <output-test url="file://$MODULE_DIR$/build/test-classes" />
    <exclude-output />
    <content url="file://$MODULE_DIR$">

on robolectric plugin source code you can see how the test-classes folder is hardcoded.

And finally our test are executed from the Android Studio and we can debug and see what is going on whenever a test fails...