开发者

Start a second activity inside a TestCase (which is not the activity under test)

How can I start a second (mock) activity in a ActivityInstrumentationTestCase2 or InstrumentationTestCase?

My Problem is this:

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

...results in the error "intent in process ... resolves to different process ...test". Using getTargetContext() for the Intent results in "unable to resolve activity for Intent", as my mock class is not part of the app package.

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.r开发者_高级运维unTest(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 -----


Test applications aren't "apps" in the traditional sense and can't start their own activities. If you need to test how your activity responds to other activities sending it intents, you can use the ActivityInstrumentationTestCase2.setActivityIntent(Intent) method to inject the various intents you want to test, before you actually call getActivity().

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
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜