簡體   English   中英

Android片段的單元測試

[英]Unit Test of Android Fragment

我正在嘗試編寫一個片段的檢測測試。 我使用了stackoverflow的示例代碼。 但我找不到此錯誤的原因:

java.lang.RuntimeException:無法解析以下活動:Intent {act = android.intent.action.MAIN flg = 0x10000000

互聯網資源表明,它可能是由以下原因引起的:

  • ActivityInstrumentationTestCase2犯規構造函數
  • Manifest.xml中的錯誤活動聲明
  • 對測試代碼使用與測試項目相同的軟件包名稱

但我認為情況並非如此。 錯誤:

 started: testFragment(de.my.androidhd.test.AccountFragmentTest)
 failed: testFragment(de.my.androidhd.test.AccountFragmentTest)
 ----- begin exception -----
 java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=de.my.androidhd/.test.TestFragmentActivity }
    at android.app.Instrumentation.startActivitySync(Instrumentation.java:371)
    at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
    at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
    at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
    at de.my.androidhd.test.AccountFragmentTest.setUp(AccountFragmentTest.java:22)
    at junit.framework.TestCase.runBare(TestCase.java:132)
    at junit.framework.TestResult$1.protect(TestResult.java:115)
    at junit.framework.TestResult.runProtected(TestResult.java:133)
    at junit.framework.TestResult.run(TestResult.java:118)
    at junit.framework.TestCase.run(TestCase.java:124)
    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)
 ----- end exception -----
 finished: testFragment(de.my.androidhd.test.AccountFragmentTest)

我有正確的Manifest.xml:

<uses-sdk android:minSdkVersion="11" />

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="de.my.androidhd" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="de.my.androidhd.test.TestFragmentActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

    <uses-library android:name="android.test.runner" />
</application>

和測試代碼:

package de.my.androidhd.test;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.test.ActivityInstrumentationTestCase2;

import de.my.androidhd.ui.AccountFragment;

public class AccountFragmentTest extends ActivityInstrumentationTestCase2<TestFragmentActivity> {
    private TestFragmentActivity mActivity;

    public AccountFragmentTest() {
        super(TestFragmentActivity.class);
    }


    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();
    }

    private Fragment startFragment(Fragment fragment) {
        FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.activity_test_fragment_linearlayout, fragment, "tag");
        transaction.commit();
        getInstrumentation().waitForIdleSync();
        Fragment frag = mActivity.getSupportFragmentManager().findFragmentByTag("tag");
        return frag;
    }

    public void testFragment() {
        AccountFragment fragment = new AccountFragment() {
            // Override methods and add assertations here.
        };

        Fragment frag = startFragment(fragment);
    }
}

所以問題是TestFragmentActivity,這意味着我們測試中使用的Activity

extends ActivityInstrumentationTestCase2<TestFragmentActivity>

在Manifest.xml中定義為targetPackage的包中必須可用:

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="de.my.androidhd" />

我的解決方案是將TestFragmentActivity移入經過測試的應用程序包中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM