2

I want to run some tests, which check, for example that activity A puts some piece of information into the intent, when it starts activity B.

Those tests require that there is Android infrastructure.

How can I implement them so that they can be run using Maven?

I tried

  1. to use akquinet archetype for this and use a separate Mave project for integration tests, but it never worked (my Robolectric test is not exeuted) and
  2. to run them in the same Maven project as the main app, which result in ClassNotFoundErrors caused by Robolectric (when I add Roboelectric to the dependencies, I can't mvn install my project because I get classpath-related errors for non-Robolectric classes).

Update 1 (23.08.2013): POM of the parent project:

<?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 of the actual app project:

<?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 of the integration test project:

<?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>
4

2 回答 2

3

请查看 Github Android App 源代码。https://github.com/github/android

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

希望能帮助到你。

于 2013-08-28T07:23:36.563 回答
1

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

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

于 2013-11-27T04:44:38.400 回答