2017-04-03 69 views
-1

我已經在android中使用espresso和ui automation編寫了測試用例。現在,當我運行我的兩個不同類的測試用例時,它總是給出錯誤套件類不使用junit4在espresso中運行第二個測試類android

java.lang.RuntimeException:找不到活動。您是否忘記通過調用getActivity()或startActivitySync或類似方法來啓動該活動?

並且不運行MainActivity2Test類。我無法理解我做錯了什麼。搜索有關此錯誤也沒有找到任何解決方案。請幫幫我。

AllTestsSuite.java

@RunWith(Suite.class) 
@Suite.SuiteClasses ({ 
     MainActivityTest.class, 
     MainActivity2Test.class, 

}) 
public class AllTestsSuite { 

} 

MainActivity.java

@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
@RunWith(AndroidJUnit4.class) 
public class MainActivityTest { 
    UiDevice device; 

    @Rule 
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false); 

    @Test 
    public void test1_doLogin(){ 
     device = UiDevice.getInstance(getInstrumentation()); 
     device.pressHome(); 
     device.wait(hasObject(desc("Apps")), 3000); 
     UiObject2 appsButton = device.findObject(desc("Apps")); 
     appsButton.click(); 
     UiObject2 appsButton1 = device.findObject(desc("MyApp")); 
     appsButton1.click(); 
test2_emaillPass(); 
} 
@Test 
    public void test2_emaillPass(){ 
} 

} 

MainActivity2Test.java

@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
@RunWith(AndroidJUnit4.class) 
public class MainActivity2Test { 
    UiDevice device; 
    @Rule 
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false); 

    @Test 
    public void test3_productFamily(){ 
     onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); 
     test4_Product(); 


    } 
    @Test 
    public void test4_Product() { 
     device = UiDevice.getInstance(getInstrumentation()); 
     onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); 
     device.wait(hasObject(By.text("First product")), 4000); 
     test5_Detail(); 
    } 

回答

0

此行是你的問題:

@Rule 
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false); 

最後false參數狀態,如果活動應該在測試開始時推出,看到方法簽名:

public ActivityTestRule(Class<T> activityClass, boolean initialTouchMode, 
     boolean launchActivity) 

所以,你要麼是最後一個標誌更改爲true或您手動啓動像每一個測試你的活動這樣的:

mActivityRule.launchActivity(null); 
+0

最後一個標誌更改爲TRUE,但仍然得到錯誤 –

+0

通過使最後一個標誌爲true,應用程序越來越多刷新也times.And拋出空指針異常 –

相關問題