构建Android应用程序并使用Maven运行Robotium测试

时间:2012-11-17 15:42:31

标签: android maven jenkins robotium

我真的需要你的帮助。我想设置Jenkins来构建我的Android应用程序并使用Maven在模拟器上运行我的Robotium测试。我想我做的一切都还不错,但仍然有同样的错误。你有没有这样的东西?我的项目结构如下:

| Z | - mobiler.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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pl.araneo.farmadroid</groupId>
    <artifactId>izi-mobiler</artifactId>
    <version>366</version>
    <packaging>pom</packaging>
    <name>IZI</name>

    <modules>
        <module>izi-mobiler-app</module>
        <module>izi-mobiler-test</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>

        <dependencies>

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

            <dependency>
                <groupId>com.google.android</groupId>
                <artifactId>android-test</artifactId>
                <version>4.0.1.2</version>
            </dependency>

            <dependency>
                <groupId>com.pivotallabs</groupId>
                <artifactId>robolectric</artifactId>
                <version>1.2-SNAPSHOT</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
                <version>1.2</version>
                <scope>test</scope>
            </dependency>

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

            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-lgpl</artifactId>
                <version>1.7.1</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>1.9.0</version>
            </dependency>

            <dependency>
                <groupId>org.roboguice</groupId>
                <artifactId>roboguice</artifactId>
                <version>2.0</version>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>com.google.inject.extensions</groupId>
                <artifactId>guice-multibindings</artifactId>
                <version>3.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.inject</groupId>
                        <artifactId>guice</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>com.google.inject.extensions</groupId>
                <artifactId>guice-assistedinject</artifactId>
                <version>3.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.inject</groupId>
                        <artifactId>guice</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

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

            <!-- Action Bar sherlock -->
            <dependency>
                <groupId>com.actionbarsherlock</groupId>
                <artifactId>library</artifactId>
                <version>4.1.0</version>
                <type>apklib</type>
            </dependency>

            <!-- Let Roboguice and Sherlock work together -->
            <dependency>
                <groupId>com.github.rtyley</groupId>
                <artifactId>roboguice-sherlock</artifactId>
                <version>1.4</version>
            </dependency>

        </dependencies>

    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.0.0-alpha-13</version>
                    <extensions>true</extensions>
                    <configuration>
                        <sdk>
                            <platform>15</platform>
                        </sdk>
                        <undeployBeforeDeploy>true</undeployBeforeDeploy>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

| Z | - mobiler-app.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>

<parent>
    <groupId>pl.araneo.farmadroid</groupId>
    <artifactId>izi-mobiler</artifactId>
    <version>366</version>
    <relativePath>../</relativePath>
</parent>

<groupId>pl.araneo.farmadroid</groupId>
<artifactId>izi-mobiler-app</artifactId>
<version>366</version>
<packaging>apk</packaging>
<name>IZI - App</name>

<properties>
    <platform.version>4.0.1.2</platform.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

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

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

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <scope>test</scope>
    </dependency>

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

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-lgpl</artifactId>
        <version>1.7.1</version>
        <type>jar</type>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.roboguice</groupId>
        <artifactId>roboguice</artifactId>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.google.inject.extensions</groupId>
        <artifactId>guice-multibindings</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.google.inject</groupId>
                <artifactId>guice</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.inject.extensions</groupId>
        <artifactId>guice-assistedinject</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.google.inject</groupId>
                <artifactId>guice</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

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

    <!-- Action Bar sherlock -->
    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>library</artifactId>
        <type>apklib</type>
    </dependency>

    <!-- Let Roboguice and Sherlock work together -->
    <dependency>
        <groupId>com.github.rtyley</groupId>
        <artifactId>roboguice-sherlock</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.4.0</version>
            <configuration>
                <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                <resourceDirectory>${project.basedir}/res</resourceDirectory>
                <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                <sdk>
                    <platform>15</platform>
                </sdk>
                <undeployBeforeDeploy>true</undeployBeforeDeploy>
            </configuration>
            <extensions>true</extensions>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

| Z | - mobiler-的test.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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>pl.araneo.farmadroid</groupId>
    <artifactId>izi-mobiler</artifactId>
    <version>366</version>
</parent>

<groupId>pl.araneo.farmadroid</groupId>
<artifactId>izi-mobiler-test</artifactId>
<packaging>apk</packaging>
<name>IZI - Test</name>

<dependencies>

    <dependency>
        <groupId>com.jayway.android.robotium</groupId>
        <artifactId>robotium-solo</artifactId>
        <version>1.8.0</version>
    </dependency>

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

    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android-test</artifactId>
        <scope>provided</scope>
    </dependency>

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

    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>library</artifactId>
        <type>apklib</type>
    </dependency>

    <dependency>
        <groupId>com.github.rtyley</groupId>
        <artifactId>roboguice-sherlock</artifactId>
    </dependency>

    <dependency>
        <groupId>pl.araneo.farmadroid</groupId>
        <artifactId>izi-mobiler-app</artifactId>
        <version>366</version>
        <type>apk</type>
    </dependency>

    <dependency>
        <groupId>pl.araneo.farmadroid</groupId>
        <artifactId>izi-mobiler-app</artifactId>
        <version>366</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>

</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <extensions>true</extensions>
        </plugin>
    </plugins>
</build>

堆栈跟踪

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[7,7] error: cannot access TestCase
[ERROR]   class file for junit.framework.TestCase not found
/home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[17,24] error: cannot find symbol
[ERROR]  class DashboardViewTest
/home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[17,46] error: cannot find symbol
[ERROR]  class DashboardViewTest
/home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[15,4] error: method does not override or implement a method from a supertype
[INFO] 4 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] IZI ............................................... SUCCESS [0.183s]
[INFO] IZI - App ......................................... SUCCESS [36.895s]
[INFO] IZI - Test ........................................ FAILURE [3.741s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 41.924s
[INFO] Finished at: Sat Nov 17 16:40:51 CET 2012
[INFO] Final Memory: 35M/354M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project izi-mobiler-test: Compilation failure: Compilation failure:
[ERROR] /home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[7,7] error: cannot access TestCase
[ERROR] class file for junit.framework.TestCase not found
[ERROR] /home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[17,24] error: cannot find symbol
[ERROR] class DashboardViewTest
[ERROR] /home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[17,46] error: cannot find symbol
[ERROR] class DashboardViewTest
[ERROR] /home/piotrslesarew/git/izi-mobiler/izi-mobiler-test/src/main/java/pl/araneo/farmadroid/ui/activities/test/DashboardViewTest.java:[15,4] error: method does not override or implement a method from a supertype
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :izi-mobiler-test

1 个答案:

答案 0 :(得分:3)

由于Instrumentation Tests在他们自己的模块中,并且应该位于'src / main / java'目录而不是'src / main / test'中,因此它们似乎在与通常的测试不同的生命周期阶段进行编译。

我通过将测试模块的junit范围从test设置为compile来消除了我的项目中的这个特殊错误:

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

(之后我的测试无法在模拟器上运行)