Search code examples
javaandroidm2eclipserobolectricandroid-maven-plugin

Roboelectric, Maven and the plight of the output folder


Dear awesome community,

I have decided to bite the bullet and put together some proper TDD/BDD testing infrastructure for my Android application. I am looking to incorporate Roboelectric in my separate test project in order prevent me from having to launch an emulator for anything remotely Android-y. To see what's what I have imported into my (Eclipse ADT 3.8) environment the RoboElectricSample project found here. But I've hit a snag... everything is working swimmingly compilation wise and I've worked around the familiar /bin, /target maven-eclipse stand-off with the answer here which I can't upvote enough.

However, out of the sample project's tests (all 87 of them) only 7 pass with the remainder falling over with;

Caused by: java.lang.ClassNotFoundException: com.pivotallabs.R
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:171)
at org.robolectric.AndroidManifest.getRClass(AndroidManifest.java:100)

Currently my src/test/java build output location is relative at target/test-classes with my src/main/java build output location at target/classes.

For completeness here is the project structure;

RoboelectricSample project structure

This Q&A exchange seems to suggest I might be barking up the wrong tree but that was over a year ago now and the m2e-android plugin has come a long way. Finally, this question seems to reflect my problem exactly but alas with no resolution.

Any help you can provide with fixing my build path to get the /gen/r file to be recognised under the Maven equivalent generated-sources/r most appreciated. I'd like to get the sample app up and running before switching my app over to Roboelectric.


Solution

  • Got it.

    Having read this discussion against the m2e-android project I added the android.jar to the test class path as described here and added the bin\classes modification to the test class path as described here. I also removed some overriding dependencies that were now being pulled in by Robolectric (dexmaker for example) and viola, I have Android tests running Dagger DI'ed code alongside Mockito.

    Life is better for TDD now.

    Game on!