2017-10-15 3 views
0

내 응용 프로그램에 대한 테스트 사례를 작성하는 중입니다. 활동에 적합합니다. 그러나 ViewPager 내부의 Fragment에 대해서는 내 사례가 실패합니다.Android Espresso Fragments 및 그 자녀의 사례를 Acitivty에서 작성하는 방법은 무엇인가요?

다음을 직접 실행하면 조각에 빈보기가 표시되므로 테스트 케이스가 실패합니다.

공용 클래스 SentimentsFragmentEspressoTest {

@Rule 
public FragmentTestRule<SentimentsFragment> mFragmentTestRule = new FragmentTestRule<>(SentimentsFragment.class); 

@Test 
public void fragment_can_be_instantiated() { 

    // Launch the activity to make the fragment visible 
    mFragmentTestRule.launchActivity(null); 

    // Then use Espresso to test the Fragment 

    onView(withId(R.id.listSentiments)) 
      .perform(RecyclerViewActions.scrollToPosition(4)); 
} } 

그래서 사람이 어떻게이 문제를 온 이상 말해 줄 수 있습니까? 감사의 ViewPager 내부 조각에 대한 K. 인 Rajesh

+0

아마도 'launchActivity (null);'을 호출했기 때문에 뷰가 비어 있거나 null이 될 가능성이 있습니다. –

+0

@ H.Brooks, launchActivity()에는 Intent 매개 변수가 있습니다. 조각을 보내는 방법? – itsrajesh4uguys

답변

0

내가 내 위의 질문에 대한 해결책을 발견하여 테스트

@Rule 
public ActivityTestRule<YourActivity> mActivityRule = new ActivityTestRule<>(YourActivity.class); 

@Before 
public void setUp() throws Exception { 

    //get fragment 
    mActivityRule.getActivity() 
      .getSupportFragmentManager().beginTransaction(); 


} 

@Test 
public void testRecyclerView() { 

    //click on item recyclerview 
    onView(withId(R.id.listSentiments)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); 
    .... 
    ... 
+0

행운입니다. 그것은 예외를 제공합니다 ** java.lang.NullPointerException : null 객체 참조에 가상 메소드 'android.support.v4.app.FragmentManager com.MyActivity.getSupportFragmentManager()'**를 호출하려고 시도했습니다. – itsrajesh4uguys

0

에 사용하려고합니다. 다음은 코드 스 니펫입니다.

ViewInteraction recyclerView = onView(
         allOf(withId(R.id.listSentiments), 
           withParent(withId(R.id.viewpager)), 
           isDisplayed())); 
       recyclerView.perform(actionOnItemAtPosition(3, click())); 

이렇게하면 Viewpager와 연결된 모든 조각에 액세스 할 수 있습니다.

관련 문제