5

ActivityInstrumentationTestCase2またはInstrumentationTestCaseで2番目のアクティビティを開始するにはどうすればよいですか? 「異なるプロセス...テストに解決...プロセスにおける意思」TestCase(テスト中のアクティビティではない)内で2番目のアクティビティを開始する

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

...エラーが発生:

私の問題はこれです。 私のモッククラスは、アプリケーションパッケージの一部ではないため、インテントのgetTargetContext()を使用すると「インテントのアクティビティを解決できません」という結果になります。

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 
    } 
} 
関連する問題