Android Shorts 🩳: Testing Toasts with Espresso

On a long enough timeline, every test is a flaky test

Costa Fotiadis
Apr 17, 2023

Now why would anyone use this?

  • Toasts cover the UI for a while, preventing other actions and matchers from working.
  • Time sensitive.
  • We are in compose world and..
  • Matchers suck. 🫠

The only good toast is French Toast

Create a generic Toaster interface that will be responsible for all toasts in the app. Install the implementation into the SingletonComponent :

Add the hilt-testing dependency

Time to leverage TestInstallIn.

Override

Any action that should have shown a Toast before, will now add an element in a list instead when running UI tests.

In reality

This will just show a good old Toast when ran normally.

In testing

Anyways

@ costafotiadis.com, twitter

Later.

--

--