2

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.

Community
  • 1
  • 1
BrantApps
  • 6,362
  • 2
  • 27
  • 60

2 Answers2

1

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!

BrantApps
  • 6,362
  • 2
  • 27
  • 60
  • Gah! Getting Maven to do this has been pretty difficult and thus far I have failed. Another question for another time. Hopefully by the time I need this running under CI there may be more than one of me developing. :) – BrantApps Jun 09 '13 at 23:53
0

Try something like following in your pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>gen/r/main/java</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-resource</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>gen/r/src/main/resources</directory>
                                    <targetPath>resources</targetPath>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Chris
  • 5,584
  • 9
  • 40
  • 58
  • Hi Chris, thank you very much for your response. I was hoping that having just checked out the sample Robolectric project the solution would be configuration rather than code/XML based. I am aware your recommended change is dependency configuration but shouldn't these samples work out-of-the-box? The latest [Travis CI build](http://ci.robolectric.org) seems to think everything is a-OK. Could it be that the Eclipse JUnit runner just isn't up to the job and Maven has to be performing the builds? – BrantApps Jun 07 '13 at 08:28