0

我最近一直在尝试使用 Android Instrumentation 测试。所以主应用程序有以下结构

<?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.onestopspot</groupId>
    <artifactId>api_library</artifactId>
    <version>5.0.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>One Stop Spot</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-rest-template</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>4.1.1.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.4.0</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <configuration>
                    <sdk>
                        <platform>16</platform>
                    </sdk>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pm.xml 文件具有以下结构

http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 onestopspot parent 1.0.0-SNAPSHOT com.onestopspot 仪器 apk 一站式商店-仪器测试

<dependencies>
    <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.onestopspot</groupId>
        <artifactId>api_library</artifactId>
        <version>5.0.0-SNAPSHOT</version>
        <scope>provided</scope>
        <type>apk</type>
    </dependency>

    <dependency>
        <groupId>com.onestopspot</groupId>
        <artifactId>api_library</artifactId>
        <version>5.0.0-SNAPSHOT</version>
        <scope>provided</scope>
        <type>jar</type>
    </dependency>
</dependencies>

<profiles>
    <profile>
        <id>emma</id>
        <dependencies>
            <dependency>
                <groupId>emma</groupId>
                <artifactId>emma</artifactId>
                <type>jar</type>
                <scope>compile</scope>
                <version>2.1.5320</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.0-alpha-1</version>
                    <executions>
                        <execution>
                            <phase>initialize</phase>
                            <goals>
                                <goal>read-project-properties</goal>
                            </goals>
                            <configuration>
                                <files>
                                    <file>project.properties</file>
                                </files>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <configuration>
                        <test>
                            <coverage>true</coverage>
                            <createReport>true</createReport>
                        </test>
                    </configuration>
                    <extensions>true</extensions>
                    <!-- -->
                    <executions>
                        <execution>
                            <id>pull-coverage</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>pull</goal>
                            </goals>
                            <configuration>
                                <pullSource>/data/data/com.onestopspot.api/files/coverage.ec</pullSource>
                                <pullDestination>${tested.project.dir}/target/emma/coverage.ec</pullDestination>
                            </configuration>
                        </execution>
                    </executions>

                </plugin>
            </plugins>
        </build>
        <reporting>
            <plugins>
                <plugin>
                    <groupId>org.sonatype.maven.plugin</groupId>
                    <artifactId>emma4it-maven-plugin</artifactId>
                    <version>1.3</version>
                    <configuration>
                        <metadatas>${tested.project.dir}/target/emma/coverage.em,${tested.project.dir}/src/
                        </metadatas>
                        <instrumentations>${tested.project.dir}/target/emma/coverage.ec</instrumentations>
                        <reportDirectory>${tested.project.dir}/target/emma/</reportDirectory>
                        <baseDirectory>{tested.project.dir}/target/</baseDirectory>
                        <formats>xml,html</formats>
                    </configuration>
                </plugin>
            </plugins>
        </reporting>
    </profile>
</profiles>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>

                <test>
                    <skip>false</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> -->
                    <testSize>small</testSize>
                    <createReport>true</createReport>
                    <!--<classes> -->
                    <!--<class>your.package.name.YourTestClass</class> -->
                    <!--</classes> -->
                    <!--<packages> -->
                    <!--<package>your.package.name</package> -->
                    <!--</packages> -->
                </test>
            </configuration>
        </plugin>
    </plugins>
</build>

现在在我的主项目中,在 com.onestopspot.api 下,我有我的 android 应用程序类。在 onCreate 函数中,我有一条简单的 log.d 消息,告诉我应用程序类已创建。现在在我的测试项目中,我有一个 com.onestopspot.api 我有以下类

package com.onestopspot.api;

import junit.framework.Test;
import junit.framework.TestSuite;

import android.test.InstrumentationTestRunner;
import android.test.suitebuilder.TestSuiteBuilder;

public class AllTests extends TestSuite {

    public static Test suite() {
        return new TestSuiteBuilder(AllTests.class)
                .includeAllPackagesUnderHere()
                .build();
    }
}

现在当我说 mvn clean install 我得到以下异常

[INFO] SH28HTV04654_HTC_HTCOneV :       java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1677)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:371)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4456)
at android.app.ActivityThread.access$1300(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.onestopspot.api.MyApplication
at com.onestopspot.api.ApplicationTests.<init>(ApplicationTests.java:38)
... 18 more

我究竟做错了什么 ?我已经在这两天了。我的项目看起来与此处发现的项目几乎相同,但这行得通吗?如果有人可以帮助我,我可以发送我的代码吗?提前致谢。

03-16 19:05:11.068: I/TestRunner(9928): failed: testSuiteConstructionFailed(android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests)
03-16 19:05:11.068: I/TestRunner(9928): ----- begin exception -----
03-16 19:05:11.068: I/TestRunner(9928): java.lang.RuntimeException: Exception during suite construction
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
03-16 19:05:11.068: I/TestRunner(9928):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:05:11.068: I/TestRunner(9928):     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 19:05:11.068: I/TestRunner(9928):     at junit.framework.TestCase.runTest(TestCase.java:154)
03-16 19:05:11.068: I/TestRunner(9928):     at junit.framework.TestCase.runBare(TestCase.java:127)
03-16 19:05:11.068: I/TestRunner(9928):     at junit.framework.TestResult$1.protect(TestResult.java:106)
03-16 19:05:11.068: I/TestRunner(9928):     at junit.framework.TestResult.runProtected(TestResult.java:124)
03-16 19:05:11.068: I/TestRunner(9928):     at junit.framework.TestResult.run(TestResult.java:109)
03-16 19:05:11.068: I/TestRunner(9928):     at junit.framework.TestCase.run(TestCase.java:118)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
03-16 19:05:11.068: I/TestRunner(9928):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1677)
03-16 19:05:11.068: I/TestRunner(9928): Caused by: java.lang.reflect.InvocationTargetException
03-16 19:05:11.068: I/TestRunner(9928):     at java.lang.reflect.Constructor.constructNative(Native Method)
03-16 19:05:11.068: I/TestRunner(9928):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)
03-16 19:05:11.068: I/TestRunner(9928):     at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:371)
03-16 19:05:11.068: I/TestRunner(9928):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4456)
03-16 19:05:11.068: I/TestRunner(9928):     at android.app.ActivityThread.access$1300(ActivityThread.java:139)
03-16 19:05:11.068: I/TestRunner(9928):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
03-16 19:05:11.068: I/TestRunner(9928):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 19:05:11.068: I/TestRunner(9928):     at android.os.Looper.loop(Looper.java:156)
03-16 19:05:11.068: I/TestRunner(9928):     at android.app.ActivityThread.main(ActivityThread.java:4987)
03-16 19:05:11.068: I/TestRunner(9928):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:05:11.068: I/TestRunner(9928):     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 19:05:11.068: I/TestRunner(9928):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-16 19:05:11.068: I/TestRunner(9928):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-16 19:05:11.068: I/TestRunner(9928):     at dalvik.system.NativeStart.main(Native Method)
03-16 19:05:11.068: I/TestRunner(9928): Caused by: java.lang.NoClassDefFoundError: **com.onestopspot.api.MyApplication**
03-16 19:05:11.068: I/TestRunner(9928):     at com.onestopspot.api.APITest.<init>(APITest.java:38)
03-16 19:05:11.068: I/TestRunner(9928):     ... 18 more
03-16 19:05:11.068: I/TestRunner(9928): ----- end exception -----
03-16 19:05:11.078: I/TestRunner(9928): finished: testSuiteConstructionFailed(android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests)
03-16 19:05:11.088: I/ActivityManager(1656): Force stopping package com.onestopspot.api uid=10161

com.onestopspot.api.MyApplication 是我在主项目中的应用程序类。我的仪器仪表项目好像找不到?

4

2 回答 2

0

尝试改变这个

  <dependency>
    <groupId>com.onestopspot</groupId>
    <artifactId>api_library</artifactId>
    <version>5.0.0-SNAPSHOT</version>
    <scope>provided</scope>
    <type>apk</type>
</dependency>

<dependency>
    <groupId>com.onestopspot</groupId>
    <artifactId>api_library</artifactId>
    <version>5.0.0-SNAPSHOT</version>
    <scope>provided</scope>
    <type>jar</type>
</dependency>

<dependency>
    <groupId>com.onestopspot</groupId>
    <artifactId>api_library</artifactId>
    <version>5.0.0-SNAPSHOT</version>
    <scope>test</scope>
    <type>apk</type>
</dependency>

<dependency>
    <groupId>com.onestopspot</groupId>
    <artifactId>api_library</artifactId>
    <version>5.0.0-SNAPSHOT</version>
    <scope>provided</scope>
</dependency>
于 2014-01-14T08:36:21.567 回答
0

我曾经也有过一样的问题。事实证明,我的应用程序和我的测试项目具有相同的包名(在清单文件中)。adb 通过那里的包名称卸载并安装 apk。这组指令在执行测试之前由 adb 执行:

  • 卸载应用程序
  • 安装应用程序
  • 卸载测试(如果它们与应用程序具有相同的包名称,这将卸载您的应用程序!!)
  • 安装测试
  • 运行测试

因此,在我的项目中,当我重命名我的仪器测试包时,一切都得到了修复。

于 2014-05-21T08:50:55.907 回答