如何为mavenised Android应用程序运行集成测试?

时间:2013-08-11 14:03:30

标签: java android maven testing robolectric

我想运行一些测试,例如,当它启动活动B时,活动A将一些信息放入意图中。

这些测试要求有Android基础设施。

如何实现它们以便可以使用Maven运行?

我试过

  1. 为此使用akquinet原型并使用单独的Mave项目进行集成测试,但是it never worked(我的Robolectric测试没有被执行)和
  2. 在与主应用程序相同的Maven项目中运行它们,这导致由Robolectric引起的ClassNotFoundErrors(当我将Roboelectric添加到依赖项时,我不能mvn install我的项目,因为我得到非Robolectric类的类路径相关错误。)
  3. 更新1(23.08.2013):父项目的POM:

    <?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.mycompany</groupId>
        <artifactId>my-product-parent</artifactId>
        <version>1.3.0</version>
        <packaging>pom</packaging>
        <name>my-product - Parent</name>
    
        <modules>
            <module>my-product</module>
            <module>my-product-it</module>
        </modules>
    
        <properties>
            <platform.version> 4.1.1.4
                        </platform.version>
            <android.plugin.version>3.6.0</android.plugin.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.google.android</groupId>
                    <artifactId>android</artifactId>
                    <version>${platform.version}</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>com.google.android</groupId>
                    <artifactId>android-test</artifactId>
                    <version>${platform.version}</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.11</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                        <artifactId>android-maven-plugin</artifactId>
                        <version>${android.plugin.version}</version>
                        <configuration>
                            <sdk>
                                <platform>16</platform>
                            </sdk> 
                                                </configuration>
                    </plugin>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.1</version>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    实际应用项目的POM:

    <?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>com.mycompany</groupId>
            <artifactId>my-product-parent</artifactId>
            <version>1.3.0</version>
        </parent>
    
        <groupId>com.mycompany</groupId>
        <artifactId>my-product</artifactId>
        <version>1.4.0</version>
        <packaging>apk</packaging>
        <name>my-product - Application</name>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <powermock.version>1.5</powermock.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>com.google.android</groupId>
                <artifactId>android</artifactId>
                <version>${platform.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.mycompany</groupId>
                <artifactId>some-library</artifactId>
                <version>1.4.0</version>
            </dependency>
            <dependency>
                <groupId>com.mycompany</groupId>
                <artifactId>commons</artifactId>
                <version>2.5</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.inject</groupId>
                        <artifactId>guice</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.1.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.1.4</version>
            </dependency>
            <!-- Testing (start) -->
            <dependency>
                <groupId>org.easytesting</groupId>
                <artifactId>fest-assert-core</artifactId>
                <version>2.0M8</version>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>1.9.5</version>
                <scope>test</scope>
    
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>${powermock.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito</artifactId>
                <version>${powermock.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
    
            <!-- Testing (end) -->
            <!-- Google Maps (start) -->
            <!-- 
            <dependency>
                <groupId>com.google.android.maps</groupId>
                <artifactId>maps</artifactId>
                <version>16_r3</version>
                <scope>provided</scope>
            </dependency>
    
    
     -->
            <dependency>
                <groupId>com.google.android.gms</groupId>
                <artifactId>google-play-services</artifactId>
                <version>6</version>
                <type>apklib</type>
            </dependency>
    
            <dependency>
                <groupId>com.google.android.gms</groupId>
                <artifactId>google-play-services</artifactId>
                <version>6</version>
                <type>jar</type>
            </dependency>
            <!-- Google Maps (end) -->
        </dependencies>
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    集成测试项目的POM:

    <?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>com.mycompany</groupId>
            <artifactId>my-product-parent</artifactId>
            <version>1.3.0</version>
        </parent>
    
        <artifactId>my-product-it</artifactId>
        <packaging>apk</packaging>
        <name>my-product-it - Integration tests</name>
    
        <dependencies>
            <dependency>
                <groupId>com.google.android</groupId>
                <artifactId>android-test</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.mycompany</groupId>
                <artifactId>my-product</artifactId>
                <type>apk</type>
                <version>1.3.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.mycompany</groupId>
                <artifactId>my-product</artifactId>
                <type>jar</type>
                <version>1.3.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>1.9.5</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.hamcrest</groupId>
                        <artifactId>hamcrest-core</artifactId>
                    </exclusion>                
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.easytesting</groupId>
                <artifactId>fest-assert-core</artifactId>
                <version>2.0M8</version>
            </dependency>
    
            <!-- Make sure this (robolectric dependency) is below the android dependencies -->
            <dependency>
                <groupId>com.pivotallabs</groupId>
                <artifactId>robolectric</artifactId>
                <version>1.0-RC4</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.inject</groupId>
                        <artifactId>guice</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.objenesis</groupId>
                        <artifactId>objenesis</artifactId>
                    </exclusion>
                    <!-- 
                    <exclusion>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                    </exclusion>
                     -->
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <configuration>
                        <test>
                            <!--<skip>true|false|auto</skip> -->
                            <!--<instrumentationPackage>packageName</instrumentationPackage> -->
                            `
                            <!--<instrumentationRunner>className</instrumentationRunner> -->
                            <!--<debug>true|false</debug> -->
                            <!--<coverage>true|false</coverage> -->
                            <!--<logonly>true|false</logonly> avd -->
                            <!--<testsize>small|medium|large</testsize> -->
                            <createReport>true</createReport>
    
                            <classes>
                                <class>com.mycompany.cb.android.test.AcceptInvitationActivityTest</class>
                            </classes>
    
                            <!--<classes> -->
                            <!--<class>your.package.name.YourTestClass</class> -->
                            <!--</classes> -->
                            <!--<packages> -->
                            <!--<package>your.package.name</package> -->
                            <!--</packages> -->
                        </test>
                    </configuration>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </build>
    </project>
    

2 个答案:

答案 0 :(得分:3)

结帐Github Android App源代码。 https://github.com/github/android

这个项目有一个非常好的maven设置和集成测试。他们使用Roboguicemaven-android-plugin

希望它有所帮助。

答案 1 :(得分:1)

该项目集成了许多测试工具以及代码质量工具:https://github.com/stephanenicolas/Quality-Tools-for-Android

我写了一个bash脚本,生成了一个配置了robolectric,spoon和robotium的mavenized android项目:https://github.com/marsucsb/mvn-android-project