12

I've been banging my head against a wall for some time trying to get maven, robolectric, actionbar sherlock, and intellij, to all play nicely. I've given up on building with intellij for now and am focusing on maven.

I have an app with an action bar when I skip the tests in the maven build (which tells me I'm good to go on the ActionBar integration front). The IntelliJ build fails on ManifestParsingTest.java complaining that it doesn't know where junit is (among other issues).

Robolectric integration

I already had Robolectric running successfully in the project. I copied the files from Jake Wharton's gist into src/test/java/com/blah/blah/support/ (except ShadowSherlockFragmentActivity.java which lives in src/test/java/com/blah/blah/support/shadows/) and put this line in the constructor for my custom test runner (lives in support folder):

ActionBarSherlock.registerImplementation(ActionBarSherlockRobolectric.class);

mvn errors

I'm running into these build errors (IntelliJ also has red squigglies for the same stuff):

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:testCompile (default-testCompile) on project Blah: Compilation failure: Compilation failure:
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/SherlockResourceLoader.java:[29,9] cannot find symbol
[ERROR] symbol  : constructor ResourceLoader(int,java.lang.Class,java.util.List<java.io.File>,java.io.File)
[ERROR] location: class com.xtremelabs.robolectric.res.ResourceLoader
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/SherlockResourceLoader.java:[45,5] method does not override or implement a method from a supertype
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[39,37] resourceLoaderForRootAndDirectory has private access in com.xtremelabs.robolectric.RobolectricTestRunner
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[47,30] cannot find symbol
[ERROR] symbol  : method getResourcePath()
[ERROR] location: class com.xtremelabs.robolectric.RobolectricConfig
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[48,9] resourceLoaderForRootAndDirectory has private access in com.xtremelabs.robolectric.RobolectricTestRunner
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[38,3] method does not override or implement a method from a supertype

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.blah.blah</groupId>
    <artifactId>projectName</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>Project Name App</name>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>4.0.1.2</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.pivotallabs</groupId>
            <artifactId>robolectric</artifactId>
            <version>1.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.8.2</version>
             <scope>test</scope>
        </dependency>

        <dependency>
             <groupId>com.google.android</groupId>
             <artifactId>support-v4</artifactId>
             <version>r6</version>
        </dependency>

        <dependency>
            <groupId>com.actionbarsherlock</groupId>
            <artifactId>library</artifactId>
            <version>4.1.0</version>
            <type>apklib</type>
        </dependency>
    <build>
        <finalName>${project.artifactId}</finalName>

        <plugins>
            <plugin>
                <!-- See http://code.google.com/p/maven-android-plugin/ -->
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <sdk>
                        <platform>15</platform>
                    </sdk>
                </configuration>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

Is there some project config I'm missing?

colabug
  • 2,602
  • 2
  • 22
  • 28
  • Do you get the same error with the 1.2-SNAPSHOT version of robolectric? – Benjamin Cassidy Dec 05 '12 at 17:03
  • @redwoolf: Setting the version to 1.2-SNAPSHOT in the pom gave this error: "The POM for com.pivotallabs:robolectric:jar:1.2-SNAPSHOT is missing, no dependency information available." Is there another way I can get the snapshot? – colabug Dec 05 '12 at 17:16
  • Yes you need to add the snapshot repo to your pom or in your settings. see http://stackoverflow.com/a/7717234/112263 – Benjamin Cassidy Dec 06 '12 at 18:43
  • Thanks for the link. I updated my settings, but it's still failing. That's because square's changes have not been merged into the mainline Robolectric (see answer below). – colabug Dec 06 '12 at 18:50
  • Are you trying to: 1. get an android "hello world" project to work with actionbarsherlock / robolectic / maven OR 2. add those to an existing project and test certain logic on the JVM where actionbarsherlock classes are instantiated? – gnorsilva Dec 13 '12 at 08:18
  • @gnorsilva The latter - integrating into an existing project. Integration breaks the existing test suite, so I haven't started testing the actionbar itself. – colabug Dec 13 '12 at 12:56
  • @colabug how come you have to do `ActionBarSherlock.registerImplementation(ActionBarSherlockRobolectric.class);` ? Also you can remove the dependency on the support library. ABS already brings that in. – Blundell Dec 13 '12 at 15:40

2 Answers2

10

At Square, to support this use case we use a modified version of Robolectric. We are working with Pivotal to get us off of using a fork and the necessary changes upstream into the main repository. The changes required are hooks into the resource loading system. These methods are currently private which is the cause of the compilation failures that you are seeing.

Pivotal doesn't exactly make releases in any semblance of a frequent manner so if the changes do make it upstream you'll likely have to use the SNAPSHOT version for a while.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • Thanks for the quick answer! It sounds like my options are to use square's branch instead of the mainline or manually patch a locally forked version of mainline Robolectric. – colabug Dec 05 '12 at 17:09
  • 1
    I was just told we are now 99.9% synchronized with upstream Robolectric and should be able to switch over to their version (the `master` branch) in the next week or two. So hopefully we can press them to make a release once that's completed. – Jake Wharton Dec 05 '12 at 17:36
  • @JakeWharton do you use Eclipse or IntelliJ as an ide ? I ask you that as release 4.2.0 has some troubles related to support library under Eclipse. It is included both in pom and libs folder and is dexed twice by eclipse. – Snicolas Dec 06 '12 at 04:34
  • 1
    IntelliJ. If there's an Eclipse/Maven interaction bug then it's in m2e or m2e-android. – Jake Wharton Dec 06 '12 at 16:26
  • 1
    Link no longer works. Look at the answer below: http://stackoverflow.com/a/14260451/1819402 – MAGx2 Dec 19 '13 at 00:17
2

I could make it work.

Try this:

  • move robolectric and junit dependencies to the end.

  • indicate src and test path in the build tag, after finalname and before plugins.

  • rebuild and run test from maven toolbar.

    <dependencies>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>4.0.1.2</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>support-v4</artifactId>
        <version>r6</version>
    </dependency>
    
    <dependency>
        <groupId>org.roboguice</groupId>
        <artifactId>roboguice</artifactId>
        <version>2.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.octo.android.robospice</groupId>
        <artifactId>robospice-spring-android</artifactId>
        <version>1.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.9</version>
    </dependency>
    
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.2</version>
    </dependency>
    
    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>actionbarsherlock</artifactId>
        <version>4.2.0</version>
        <type>apklib</type>
    </dependency>
    
    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>actionbarsherlock</artifactId>
        <version>4.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>1.1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    
    </dependencies>
    
    <build>
    <finalName>${project.artifactId}</finalName>
    
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>test</testSourceDirectory>
    
    <plugins>
        <plugin>
            <!-- See http://code.google.com/p/maven-android-plugin/ -->
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <sdk>
                    <platform>15</platform>
                </sdk>
            </configuration>
            <extensions>true</extensions>
        </plugin>
    </plugins>
    </build>
    
gabrielkou
  • 76
  • 6