Robolectric과 PowerMock(EasyMock)을 이용한 AlarmReceiver 테스트
안드로이드 2010/12/02 22:56Robolectric을 이용하여 큐픽(안드로이드 앱)에 테스트를 붙여 보고 있습니다.
사실 테스트를 시작한 이유가 GPS 문제를 해결해 보고자 하는 것이어서 열린약국찾기에 테스트를 붙여 보려 했으나,
내부 구조가 (제 실력으로) 테스트를 붙이기에 수월하게 되어 있지 않아 우선은 큐픽으로 연습을 해 보는 것입니다.
큐픽에서는 일정 시간마다 서버에 최신 사진이나 글이 올라와 있는지 체크를 하는 기능이 있습니다.
여기에 테스트를 붙여 보았습니다.
@RunWith(RobolectricTestRunner.class)
public class AlarmReceiverTest {
Context mockContext;
SharedPreferences mockPrefs;
NotificationManager mockNotificationManager;
NetworkHelper mockNetworkHelper;
AlarmReceiver receiver;
@Before
public void SetUp() {
receiver = new AlarmReceiver();
mockPrefs = PowerMock.createMock(SharedPreferences.class);
mockNotificationManager = PowerMock.createMock(NotificationManager.class);
mockNetworkHelper = PowerMock.createMock(NetworkHelper.class);
NetworkHelper.setInstance(mockNetworkHelper);
Date now = new Date();
long fiveMinAgo = now.getTime() - 5 * 60 * 1000;
expect(mockPrefs.getBoolean("auto_update_vibrate", true)).andReturn(true);
// 가장 최근에 확인한 시간을 5분 전으로 세팅
expect(mockPrefs.getLong("lastCheckedDate", 0)).andReturn(fiveMinAgo);
replay(mockPrefs);
// 가짜 pref와 notificationManager를 반환하는 context 생성
mockContext = PowerMock.createMock(Context.class);
expect(mockContext.getSharedPreferences("pref", Context.MODE_PRIVATE)).andReturn(mockPrefs);
expect(mockContext.getSystemService(Context.NOTIFICATION_SERVICE)).andReturn(mockNotificationManager);
replay(mockContext);
}
@After
public void TearDown() {
// NetworkHelper를 정리.
NetworkHelper.setInstance(null);
}
// 마지막으로 체크한지 5분이 지났는데 사진이 1개 올라온 경우 알림이 울리는지 체크
@Test
public void TestOnReceive_1photo_during5min() {
// 얻어오는 사진이 1장 있음
expect(mockNetworkHelper.GetCountOfNewPhotos(EasyMock.anyObject(Date.class))).andReturn(new NewContentInfo(1, 0));
replay(mockNetworkHelper);
// NotificationManager에 notify가 한번 불려야 한다.
mockNotificationManager.notify(EasyMock.anyInt(), EasyMock.anyObject(Notification.class));
replay(mockNotificationManager);
receiver.onReceive(mockContext, null);
EasyMock.verify(mockNotificationManager);
}
// 5분 동안 사진이 안올라 온 경우 알람이 울리는지 체크
@Test
public void TestOnReceive_0photo_during5min() {
// 얻어오는 사진이 0장 있음
expect(mockNetworkHelper.GetCountOfNewPhotos(EasyMock.anyObject(Date.class))).andReturn(new NewContentInfo(0, 0));
replay(mockNetworkHelper);
// NotificationManager에 notify가 불리면 안된다.
replay(mockNotificationManager);
receiver.onReceive(mockContext, null);
EasyMock.verify(mockNotificationManager);
}
// 5분 동안 댓글만 하나 올라 온 경우 알람이 울리는지 체크
@Test
public void TestOnReceive_1comment_during5min() {
// 얻어오는 사진이 0장 있음
expect(mockNetworkHelper.GetCountOfNewPhotos(EasyMock.anyObject(Date.class))).andReturn(new NewContentInfo(0, 1));
replay(mockNetworkHelper);
// NotificationManager에 notify가 한번 불려야 한다.
mockNotificationManager.notify(EasyMock.anyInt(), EasyMock.anyObject(Notification.class));
replay(mockNotificationManager);
receiver.onReceive(mockContext, null);
EasyMock.verify(mockNotificationManager);
}
}
아주 단순한 것이지만, 이렇게 테스트를 해볼 수 있다는 것이 신기하네요.
점점 더 많은 테스트를 붙여서 커버리지를 넓혀봐야겠습니다.
'안드로이드' 카테고리의 다른 글
| Robolectric과 PowerMock(EasyMock)을 이용한 AlarmReceiver 테스트 (1) | 2010/12/02 |
|---|---|
| 안드로이드에서의 유닛 테스트 : Robolectric을 소개합니다. (1) | 2010/11/30 |
| Cupic [큐픽] - 트위터 기반 사진 커뮤니티 앱 (0) | 2010/10/12 |
| 열린약국찾기가 8월의 으뜸앱에 선정되었습니다. (3) | 2010/08/26 |
| android must override a superclass method 해결법 (8) | 2010/08/20 |
| 우리동네병원 건의 페이지 (33) | 2010/07/21 |
