【问题标题】:Start a second activity inside a TestCase (which is not the activity under test)在 TestCase 中启动第二个活动(不是正在测试的活动)
【发布时间】:2011-08-07 14:12:32
【问题描述】:

如何在 ActivityInstrumentationTestCase2 或 InstrumentationTestCase 中启动第二个(模拟)活动?

我的问题是这样的:

Intent intent = new Intent(getInstrumentation().getContext(), MyMock.class);
myMock = (MyMock) getInstrumentation().startActivitySync(intent);

...导致错误“intent in process ... resolves to different process ...test”。 对 Intent 使用 getTargetContext() 会导致“无法解析 Intent 的活动”,因为我的模拟类不是应用程序包的一部分。

08-07 19:38:25.822: INFO/TestRunner(2656): ----- begin exception -----
08-07 19:38:25.822: INFO/TestRunner(2656): java.lang.RuntimeException: Unable to resolve activity for: Intent { cmp=com.cocktails/.test.recipes.RecipeBookMock }
08-07 19:38:25.822: INFO/TestRunner(2656):     at android.app.Instrumentation.startActivitySync(Instrumentation.java:447)
08-07 19:38:25.822: INFO/TestRunner(2656):     at com.cocktails.test.recipes.RecipeUpdaterTest.testNewRecipe(RecipeUpdaterTest.java:55)
08-07 19:38:25.822: INFO/TestRunner(2656):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 19:38:25.822: INFO/TestRunner(2656):     at java.lang.reflect.Method.invoke(Method.java:521)
08-07 19:38:25.822: INFO/TestRunner(2656):     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:191)
08-07 19:38:25.822: INFO/TestRunner(2656):     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:181)
08-07 19:38:25.822: INFO/TestRunner(2656):     at junit.framework.TestCase.runBare(TestCase.java:127)
08-07 19:38:25.822: INFO/TestRunner(2656):     at junit.framework.TestResult$1.protect(TestResult.java:106)
08-07 19:38:25.822: INFO/TestRunner(2656):     at junit.framework.TestResult.runProtected(TestResult.java:124)
08-07 19:38:25.822: INFO/TestRunner(2656):     at junit.framework.TestResult.run(TestResult.java:109)
08-07 19:38:25.822: INFO/TestRunner(2656):     at junit.framework.TestCase.run(TestCase.java:118)
08-07 19:38:25.822: INFO/TestRunner(2656):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164)
08-07 19:38:25.822: INFO/TestRunner(2656):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151)
08-07 19:38:25.822: INFO/TestRunner(2656):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:425)
08-07 19:38:25.822: INFO/TestRunner(2656):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1520)
08-07 19:38:25.832: INFO/TestRunner(2656): ----- end exception -----

【问题讨论】:

  • 使用Intent?
  • 发布完整的 StackTrace / LogCat 输出。
  • 如果您的 Activity 不是您应用程序的一部分,那么您打算如何启动它?
  • 我只是想启动另一个可以与被测 Activity 通信的 Activity ...该 Activity 的类在测试项目中(因为它是一个模拟类)

标签: android android-activity mocking instrumentation testcase


【解决方案1】:

测试应用程序不是传统意义上的“应用程序”,不能启动自己的活动。如果您需要测试您的活动如何响应发送它的意图的其他活动,您可以在实际调用 getActivity() 之前使用 ActivityInstrumentationTestCase2.setActivityIntent(Intent) 方法注入您想要测试的各种意图。

public class ExampleTest extends ActivityInstrumentationTestCase2 {

    // if your test runs on the UI thread, you will need to setActivityIntent() 
    // in setUp() as you won't have a chance to do it before the activity
    // is started

    // @UiThreadTest
    public void testMockIntent() {
        setActivityIntent(new Intent());
        Activity foo = getActivity();

        assertNotNull(foo); // your tests
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多