爲了對涉及Android狀態欄的代碼使用測試驅動的開發原則,我需要編寫測試來驗證預期狀態已經實現。例如,像下面的一個小單元測試,讓我覈實,我打算忍受通知實際上顯示:我可以爲Android狀態欄通知做TDD嗎?
public class NotificationTest extends AndroidTestCase {
public void testCanNotify() {
// setup SUT
NotificationManager mgr = (NotificationManager) getContext().getSystemService(
Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(android.R.drawable.stat_notify_error, "ticker", 0);
PendingIntent intent = PendingIntent.getActivity(getContext(), 0, null, 0);
notif.setLatestEventInfo(getContext().getApplicationContext(), "title", "text", intent);
int id = 123;
// exercise SUT
mgr.notify(id, notif);
// verify result
// QUESTION: HERE I WOULD LIKE TO SAY:
// assertTrue(mgr.isShowing(id));
// teardown
mgr.cancel(id);
}
}
因此,作爲例子顯示,通知管理器本身似乎並不具有任何診斷方法,例如isShowing(id)
或get(id)
,我可以將它們用於測試中的「驗證」步驟。
我已經看過了優秀Robotium工具包,但他們都非常具體的關於測試的一個應用程序,所以他們不涵蓋通知。
有誰知道解決方案嗎?
謝謝,這對我有意義。實際上,其目的不是測試通知管理器,而是測試我自己的各種活動之間的交叉依賴關係。我明白你的意思,這最好是在模擬的背景下完成 - 我會研究這一點。 – marc1s