2011-08-07 57 views
5

如何在ActivityInstrumentationTestCase2或InstrumentationTestCase中啓動第二個(模擬)活動?在TestCase(不是被測活動)內部開始第二個活動

我的問題是這樣的:

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

...導致錯誤「的過程意圖......解決不同的工藝......測試」。 對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 ----- 
+0

通過使用[Intent](http://developer.android.com/reference/android/app/Activity.html#StartingActivities)? –

+0

我具體化了問題 – cody

+0

發佈完整的StackTrace/LogCat輸出。 –

回答

3

測試應用程序不是傳統意義上的「應用程序」,不能開始自己的活動。如果您需要測試活動如何響應發送意圖的其他活動,則可以在實際調用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 
    } 
} 
相關問題