Code-Apprentice
Code-Apprentice

Reputation: 83567

Writing JUnit tests for Toasts in an Android app

I often use Toast.makeText().show() to display messages to the user of my Android app. These can be instructions what to do next or error messages. As part of my JUnit tests, I would like to include assertions that these messages appear when expected. How do I go about doing this?

Upvotes: 2

Views: 2431

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83567

Since originally asking this question, I have found the following solution which works well for me:

public static void waitForToast(Solo solo, String message) {
    Assert.assertTrue(solo.waitForDialogToOpen(TIME_OUT));
    Assert.assertTrue(solo.searchText(message));
}

Upvotes: 1

Uriel Frankel
Uriel Frankel

Reputation: 14622

I do it by using Robotium framework, and calling this immediately after the toast is shown:

     TextView toastTextView = solo.getText(0);
     if(toastTextView != null){
         String toastText = toastTextView.getText().toString();
         assertEquals(toastText, "Your expected text here");
     }
     //wait for Toast to close
     solo.waitForDialogToClose(5000);

Upvotes: 2

Related Questions