Roboelectric, Maven and the plight of the output folder

限于喜欢 提交于 2019-12-11 04:02:47

问题


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;

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.


回答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!




回答2:


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>


来源:https://stackoverflow.com/questions/16968558/roboelectric-maven-and-the-plight-of-the-output-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!