繁体   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