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?
Source: Tips4all
Robotium cannot interact with the notification bar. You are restricted to one application. Check FAQ for robotium, there is mention of this:
ReplyDeletehttp://code.google.com/p/robotium/wiki/QuestionsAndAnswers
Maybe not the answer you are looking for, but as usual the solution is to:
ReplyDeleteCreate 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.
Solved a similar kind of situation by storing notification detail in data store before sending and marking as acknowledged whenever user enters into application through notification bar.
ReplyDeleteAs robotium is restricted to application boundaries it can't access system data hence tested the behavior using junit/jmockit based unit test.
Try to create a Mock object by extending NotificationManager and override the notify() methods. The overridden functions can be asserted. In your test case(s), inject the Mock to the subject activity and run the tests using Android JUnit Test.
ReplyDelete