2

我一直在使用 Android 单元测试并遇到了似乎是一个路径问题。

首先我从我的项目目录中运行这个命令

android create test-project -m ../ -p tests -n RoamPayTests

这将创建一个测试项目并对其进行初始化。自动生成的 ActivityInstrumentaionTestCase2 工作正常。我可以在这里创建测试,一切正常。

注意:请注意,主要活动在 ui 包中。

package com.roamdata.roampayx;

import com.roamdata.roampayx.ui.SplashActivity;

import android.test.ActivityInstrumentationTestCase2;

/**
 * This is a simple framework for a test of an Application.  See
 * {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on
 * how to write and extend Application tests.
 * <p/>
 * To run this test, you can type:
 * adb shell am instrument -w \
 * -e class com.roamdata.roampayx.ui.SplashActivityTest \
 * com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner
 */
public class SplashActivityTest extends ActivityInstrumentationTestCase2<SplashActivity> {

    public SplashActivityTest() {
        super("com.roamdata.roampayx", SplashActivity.class);
    }

这是自动生成的 Android_Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.roamdata.roampayx.tests"
          android:versionCode="1"
          android:versionName="1.0">
    <!-- We add an application tag here just so that we can indicate that
         this package needs to link against the android.test library,
         which is needed when building test cases. -->
    <application>
        <uses-library android:name="android.test.runner" />
    </application>
    <!--
    This declares that this application uses the instrumentation test runner targeting
    the package of com.roamdata.roampayx.  To run the tests use the command:
    "adb shell am instrument -w com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner"
    -->
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.roamdata.roampayx"
                     android:label="Tests for com.roamdata.roampayx"/>
</manifest>

问题是当我尝试创建一个新的测试用例时。

package com.roamdata.roampayx;
import com.roamdata.roampayx.provider.RoamPay;
import com.roamdata.roampayx.provider.RoamPayProvider;
import android.test.ProviderTestCase2;


public class ProviderTest extends ProviderTestCase2<RoamPayProvider> {
    public ProviderTest() {
        super(RoamPayProvider.class, RoamPay.AUTHORITY);
    }
}

接下来我构建并安装,(这里没有问题)。但后来我跑了

adb shell am instrument -w com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner

最后我得到以下错误

android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:
Error in testSuiteConstructionFailed:
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:190)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
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:379)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4142)
    at android.app.ActivityThread.access$1300(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.roamdata.roampayx.provider.RoamPayProvider
    at ProviderTest.<init>(ProviderTest.java:9)
    ... 18 more

我有对 RoamPay 和 RoamPayProvider 的导入。智能感知正在工作,我正在获得这些参考。

有任何想法吗?

4

1 回答 1

1

一切都很好,除了构造函数。我把它改成

public RoamPayProviderTest() {
    super(RoamPayProvider.class, "com.roamdata.roampayx");
}

and now everything works. I guess you can use this question and answers as a nice tutorial on how to setup your unit tests.

于 2012-07-09T14:42:52.727 回答