-4

Why is my assertion failing when I am trying to assert the amount of textviews in a list ?

@Test
public void testDeleteNote() throws Exception {

   int count= getNoOfTextViews();
    // Checking if count is greater than 0.
    if (count > 0) {
        // Iterating count times
        for (int i = 0; i < count; i++)
        {
            // Checking if count is even or odd.
            if (i % 2 == 0) {
                solo.clickInList(0);
                deleteNote();
            } else {
                // Clicking Long on the 1st item in the Notes List.
                solo.clickLongInList(0);
                // Clicking on Delete String.
                solo.clickOnText(solo.getString(R.string.menu_delete));
                }
    }

    count = getNoOfTextViews();
    // Asserting all the text views are deleted.
    assertEquals(0, count);
}

  public int getNoOfTextViews() {
    // Getting the count of text views in the activity.
    ArrayList<TextView> textView = solo.getCurrentViews(TextView.class,
            solo.getCurrentViews(ListView.class).get(0));
    return textView.size();
}

The failure I am seeing is:

junit.framework.AssertionFailedError: expected:<0> but was:<1>

UPDATE: I am seeing that this passes when i debugg, it fails only when i run the test case.

srinivasv
  • 125
  • 8
  • Please add more code. We can't know what is the variable `solo` and what are the methods `deleteNote()`, `clickInList()`, `clickLongInList()` and `clickOnText()`. – Winter Feb 02 '17 at 15:06
  • was able to solve this by adding wait for list view. – srinivasv Feb 02 '17 at 15:28

2 Answers2

3

Your count variable is calculated once at the beginning of your program and then never updated. This means, when you are comparing it with 0 at the end, it still contains the old value.

All you have to do is update your count variable by calling the method again:

count = getNoOfTextViews();

Or simply

assertEquals(0, getNoOfTextViews());
Winter
  • 3,894
  • 7
  • 24
  • 56
  • my mistake i copied it wrong. I'm using assertEquals(0, getNoOfTextViews()); . This case passes when i debug and fails when i run. – srinivasv Feb 02 '17 at 14:51
1

Added a wait for list view before taking the count. This helped me solve the problem.

Thanks all.

public int getNoOfTextViews() {
    solo.waitForView(ListView.class,0,1000);
    // Getting the count of text views in the activity.
    ArrayList<TextView> textView = solo.getCurrentViews(TextView.class,
            solo.getCurrentViews(ListView.class).get(0));
    return textView.size();
}
srinivasv
  • 125
  • 8