14

我想在测试活动中测试一个片段。我TestTragmentActivity与文件androidTest一起添加。AndroidManifest.xml但是,当我尝试通过ActivityTestRule它使用此活动时,会出现以下错误:

java.lang.RuntimeException: Could not launch activity
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:371)
at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:219)
at com.bbm.ui.fragments.StickerPackListFragmentTest.testEmpty(StickerPackListFragmentTest.java:29)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1887)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.bbm.debug/com.bbm.TestFragmentActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:391)
at android.support.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:90)
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:351)
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:348)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

TestFragmentActivity只是一个空的活动。下面是文件夹AndroidManifest.xml中的。androidTest

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.bbm">

    <application
        android:label="@string/app_name"
        tools:replace="android:label">

        <activity android:name=".TestFragmentActivity"></activity>

    </application>
</manifest>

我试图获取一些关于为什么会发生这种情况的数据。下面是我的测试:

@RunWith(AndroidJUnit4.class)
public class TestFragment {

    @Rule
    public ActivityTestRule<TestFragmentActivity> rule =
            new ActivityTestRule<>(TestFragmentActivity.class, false, false);


    @Test
    public void testEmpty() {
        // Just for logging purpose
        printActivities(InstrumentationRegistry.getTargetContext(), "APP_TARGET_CONTEXT");
        printActivities(InstrumentationRegistry.getContext(), "APP_CONTEXT");

        // The launch fails....
        rule.launchActivity(null);
    }

    private void printActivities(Context context, String tag) {
        try {
            ActivityInfo[] infos = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES).activities;
            for(ActivityInfo info : infos) {
                Log.d(tag, info.name);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        Log.d(tag + "_PACKAGE_NAME", context.getPackageName());
    }

}

上面的输出是:

10-06 18:46:45.419 13030-13052/com.example.achhatra.myapplication D/APP_TARGET_CONTEXT: com.example.achhatra.myapplication.MainActivity
10-06 18:46:45.419 13030-13052/com.example.achhatra.myapplication D/APP_TARGET_CONTEXT_PACKAGE_NAME: com.example.achhatra.myapplication
10-06 18:46:45.419 13030-13052/com.example.achhatra.myapplication D/APP_CONTEXT: com.example.achhatra.myapplication.TestFragmentActivity
10-06 18:46:45.419 13030-13052/com.example.achhatra.myapplication D/APP_CONTEXT_PACKAGE_NAME: com.example.achhatra.myapplication.test

如您所见,TestFragmentActivity是在androidTest上下文中,而不是在目标上下文中。这就是ActivityTestRule无法启动它的原因。

我想知道如何TestFragmentActivity在这个测试中启动它。

4

2 回答 2

5

launchActivity在您传递自定义Intent以启动 Activity 时使用。否则,只需true在构造函数中使用,如下所示:

@Rule
    public ActivityTestRule<TestFragmentActivity> rule =
            new ActivityTestRule<>(TestFragmentActivity.class, false, true);

否则,您可以执行以下操作来传递 Activity 参数:

@RunWith(AndroidJUnit4.class) public class TestFragment {

    @Rule
    public ActivityTestRule<TestFragmentActivity> rule =
            new ActivityTestRule<>(TestFragmentActivity.class, false, false);

   private Activity launchedActivity;

    @Before
    public void setup() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.putExtra("parameter", "Value");
        launchedActivity = rule.launchActivity(intent);
   }
}
于 2016-10-06T12:04:57.720 回答
1

看起来你有不同的包。错误说com.bbm.TestFragmentActivity但是你的包管理器有com.example.achhatra.myapplication.TestFragmentActivity

我不知道你的包结构,所以我只能猜测......将你的测试 Android 清单中的包更改为com.example.achhatra.myapplication.test并将你的测试活动放在这个包中。

于 2016-10-07T05:46:38.053 回答