2016-10-24 6 views
0

현재 으로 에스프레소로 android에서 ListView을 테스트하려고합니다. listActivity.addAssociation(association); 단순히 associationArrayAdapter.add(association); 않습니다Android 에스프레소 - ListView 행 테스트

final AssociationsListActivity listActivity = getActivity(); 

    final Association association1 = new Association(ASSOCIATION_NAMES[0], ASSOCIATION_DESCRIPTIONS[0]); 
    final Association association2 = new Association(ASSOCIATION_NAMES[1], ASSOCIATION_DESCRIPTIONS[1]); 
    final Association association3 = new Association(ASSOCIATION_NAMES[2], ASSOCIATION_DESCRIPTIONS[2]); 

    listActivity.addAssociation(association1); 
    listActivity.addAssociation(association2); 
    listActivity.addAssociation(association3); 

    assertTrue(listActivity.getAssociationsList().size() == 3); 

    onView(withId(R.id.associationsListView)).check(matches(isDisplayed())); 
    onData(anything()) 
      .inAdapterView(withId(R.id.associationsListView)) 
      .atPosition(0) 
      .perform(click()); 

    assertCurrentActivityIsInstanceOf(AssociationsListActivityTest.this 
      , AssociationsPageActivity.class); 

방법 : 다음은 테스트입니다. ArrayAdapter

는 ID와 ListView (기본적으로 표시)에 요소를 추가하는 데 사용됩니다 R.id.associationsListView

나는이 테스트를 실행할 때, 나는 다음과 같은 오류 얻을 :

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view. 
Expected: is displayed on the screen to the user 
Got: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}" 

에 의해 발생 이 주장을 주석 할 때 다음과 같은 주장, 또한 onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));

, 다음 하나는 다음과 같은 예외가 발생합니다

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: ch.epfl.sweng.project:id/associationsListView'. 
... 
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: 
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user) 
Target view: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}" 

이러한 예외를 다루는 스택 오버플로 스레드를 많이 겪었으므로 이러한 테스트를 다른 동등한 방법으로 대체했지만 이러한 오류는 여전히 무엇을해야할지 모르겠다! 어떤 도움을 주셔서 감사합니다!

답변

0

무엇이 잘못되었는지 발견했습니다.

ArrayAdapter에 내 요소를 추가하는 방법은 runOnUIThread을 사용했지만 테스트가이 메서드를 기다리지 않았으므로 associationsListView은 사용자에게 표시되지 않은 것 같습니다! 그래서 나는이처럼 내 방법 listActivity.addAssociation(Association association) 고정 :

public void addAssociation(final Association association) { 
    final CountDownLatch latch = new CountDownLatch(1); 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      if (!associationList.contains(association)) { 
       associationArrayAdapter.add(association); 
      } 
      latch.countDown(); 
     } 
    }); 
    try { 
     latch.await(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    if (!associationList.contains(association)) { 
     associationList.add(association); 
    } 
} 

시험의 모든 부분은 지금 통과 :)

관련 문제