2

私は Android Unit Testing と一緒にプラグインしてきましたが、パスの問題と思われるものに遭遇しました。

まず、プロジェクトディレクトリ内からこのコマンドを実行します

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");
}

そして今、すべてが機能します。この質問と回答は、単体テストのセットアップ方法に関する優れたチュートリアルとして使用できると思います。

于 2012-07-09T14:42:52.727 に答える