Can I test status bar notifications using Android's testing framework?

defrex picture defrex · Oct 28, 2010 · Viewed 8k times · Source

I have a class that sends a status bar notification in Android. I can't find a way to test whether the notification was sent or not, which makes it very hard to write any kind of useful unit test.

Does anyone have a solution for this?

Answer

Michael Smith picture Michael Smith · Jan 9, 2012

Maybe not the answer you are looking for, but as usual the solution is to:

  • Create an interface abstracting the notification functionality.
  • Create a default implementation that delegates to the system notification API.
  • When running tests replace (or decorate) the default implementation with a mock implementation that supports testing.

This can be simplified with the following technology:

  • Dependency injection frameworks (such as RoboGuice) to streamline the choice of implementation.
  • Mocking library (such as EasyMock) to automate creation of mock implementations.