Share this
Mastering Snackbars in Jetpack Compose: A Guide to Reliable UI Testing
by Seven Peaks on Jan. 13, 2026
In modern Android development, teams increasingly rely on automated UI testing as their apps grow more complex. However, some UI components that are easy to build can be surprisingly difficult to test. Somkiat Khitwongwattana, Staff Software Engineer at LINE MAN Wongnai, recently shared his insights on one of the most common flaky test culprits: the snackbar.
Why standard snackbar tests flake
The standard way to implement a snackbar in Jetpack Compose involves using a scaffold, a SnackbarHost, and a SnackbarHostState. While this works perfectly for users, it often fails in automated CI/CD environments.
The problem is one of timing. Consider a coupon redemption flow:
- Action: A user clicks "Collect".
- First snackbar: The app shows "Applying coupon..."
- Race: If the server responds quickly, the app tries to show the second snackbar ( "Coupon redeemed!") before the first one disappears.
In a UI test, the verification for the second message often fails because the first message hasn't disappeared yet. Developers often resort to removing the verification just to make the test pass, but this compromises quality.
A test-friendly controller and container
To eliminate flakiness, use a two-part architecture that gives tests manual control over snackbar behavior: a controller to dismiss messages and a container to manage their duration.
1. The SnackbarUiTestController
-
Purpose: A registry to store SnackbarHostState instances created during app runtime so they can be dismissed programmatically by the test.
-
Key Functions:
-
registerSnackbarHostState: Adds a state to the tracking set.
-
dismissCurrentSnackbar: Manually triggers dismiss() on any active snackbar.
-
clearAllSnackbarHostStates: Cleans up after tests to prevent memory leaks.
-
2. The SnackbarHostStateProvider
To keep production code clean, this provider uses Java Reflection to check if the test controller is available.
-
If the app is running in a test environment, it automatically registers the SnackbarHostState with the controller.
-
It uses CompositionLocalProvider to make the state easily accessible throughout the UI tree.
3. The SnackbarContainer
This component solves the duration problem. In production, snackbars use short or long durations. When isRunningUiTest() returns true, the container overrides the duration to Indefinite. This ensures the snackbar stays on screen exactly as long as the test needs to verify it, never disappearing prematurely.
Implementing the test-friendly flow
With this architecture in place, your UI tests become deterministic and easy to read. Instead of hoping the timing is right, your test code follows a clear verify-and-dismiss pattern:
Kotlin
@Test |
Cautions for production
While this method is powerful, there are two critical points to bear in mind:
1. Don't leak test code: Ensure the SnackbarUiTestController is strictly in your androidTest folder. If it accidentally ships in production, your snackbars might stay on screen indefinitely for real users.
2. Complexity: In complex apps, snackbars from other components might appear during tests, interfering with your assertions. Always use a @After tear-down method to clear all host states between tests.
Stability over simplicity
By enforcing an indefinite duration during tests and using a centralized controller to dismiss messages, you can transform flaky snackbar tests into a stable part of your testing suite. This approach moves beyond hoping the UI stays still and gives developers the tools to command it.
Somkiat Khitwongwattana
Staff Software Engineer - Android at LINE MAN Wongnai
Somkiat is a passionate Android developer with over 10 years of experience in enterprise software development and as a contributor to the Thailand Android community. He previously worked as a hardware developer building embedded systems that interfaced with Android devices.
Ready to explore how AI-native development could accelerate your next project? Contact us.
Share this
- Product Development (87)
- Service Design (67)
- Data Analytics (54)
- Product Design (52)
- Industry Insights (48)
- AI Innovation (43)
- Career (32)
- Product Discovery (30)
- Product Growth (28)
- Quality Assurance (28)
- Cloud Services (25)
- Events (24)
- PR (9)
- CSR (7)
- Data (3)
- AI (1)
- Digital Product (1)
- InsurTech (1)
- January 2026 (1)
- December 2025 (6)
- November 2025 (6)
- October 2025 (4)
- September 2025 (4)
- July 2025 (2)
- June 2025 (9)
- May 2025 (5)
- April 2025 (2)
- March 2025 (3)
- February 2025 (3)
- January 2025 (3)
- December 2024 (6)
- November 2024 (4)
- September 2024 (4)
- August 2024 (3)
- July 2024 (6)
- April 2024 (1)
- March 2024 (7)
- February 2024 (14)
- January 2024 (12)
- December 2023 (9)
- November 2023 (9)
- October 2023 (2)
- September 2023 (7)
- August 2023 (6)
- June 2023 (4)
- May 2023 (4)
- April 2023 (1)
- March 2023 (1)
- November 2022 (1)
- August 2022 (4)
- July 2022 (1)
- June 2022 (5)
- April 2022 (6)
- March 2022 (4)
- February 2022 (8)
- January 2022 (4)
- December 2021 (1)
- November 2021 (2)
- October 2021 (2)
- September 2021 (1)
- August 2021 (3)
- July 2021 (1)
- June 2021 (2)
- May 2021 (1)
- March 2021 (4)
- February 2021 (5)
- December 2020 (3)
- November 2020 (1)
- June 2020 (1)
- April 2020 (1)
- January 1970 (1)