5

ActivityInstrumentationTestCase2 또는 InstrumentationTestCase에서 두 번째 (모의) 활동을 시작하려면 어떻게해야합니까? "... 과정에서 의도 다른 프로세스 ... 테스트로 확인"TestCase (테스트중인 활동이 아닌)에서 두 번째 활동 시작

Intent intent = new Intent(getInstrumentation().getContext(), MyMock.class); 
myMock = (MyMock) getInstrumentation().startActivitySync(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 (인 텐트) 메서드를 사용하여 테스트 할 다양한 인 텐트를 주입 할 수 있습니다.

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