2017-06-02 82 views
0

我有類handleFirebaseMessages。它包含功能onMessageReceived()。該功能負責接收數據和創建通知。 onMessageReceived()RemoteMessages對象接收數據。我正在爲這個函數寫一個測試。但我還沒有完全掌握。如何使android單元測試通知?

HandleFirebaseMessages

public class HandleFirebaseMessages extends FirebaseMessagingService { 
@Override 
    public void onMessageReceived(final RemoteMessage remoteMessage) { 
    final Map<String, String> data = remoteMessage.getData(); 
    //create notifications using NotificationCompat.Builder 
    } 
} 

我已經能夠編寫測試代碼的一部分。我如何完成此操作?

@Test 
public void testHandleFirebaseMessages() throws Exception { 

    Map<String,String> data=new HashMap<String,String>(); 
    data.put("id","4422"); 
    data.put("type", "1"); 
    data.put("imageUrl" , "https://image.freepik.com/free-vector/android-boot-logo_634639.jpg"); 
    data.put("smallTitle" , "DoJMA v2"); 
    data.put("smallSubTitle", "Update now from Google Play Store"); 
    data.put("ticker" , "New update for DoJMA"); 
    data.put("contentInfo" , ""); 
    data.put("link" , "https://photo2.tinhte.vn/data/avatars/l/1885/1885712.jpg?1402763583"); 
    data.put("className" , "HomeActivity"); 
    data.put("page" , "2"); 
    data.put("bigTitle" , "DoJMA Android app version 2 released!"); 
    data.put("bigSubTitle" , "Hi folks! New DoJMA update is here! Major redesigning and improvements! This app was made by the Mobile App Club.They work really hard man...and get good products"); 
    data.put("bigSummaryText" , "Update now"); 


    RemoteMessage message = new RemoteMessage.Builder("1") 
      .setMessageId("1") 
      .setData(data) 
      .build(); 
    (new HandleFirebaseMessages()).onMessageReceived(message); 

    //WHat now? 
} 

回答

-2

現在在onMessageReceived(消息)中,創建一個自定義通知。 嘗試此鏈接Custom notificatoin

希望這有助於。

1

您通過包裝Notifications API,使其可測試進行測試。首先,您需要一種將模仿的依賴關係傳遞給包裝器的構造函數的方法。你可以寫這樣的事情:

class NotificationBuilderProvider { 

    //TODO: implement Provider<NotificationCompat.Builder> if you are using JSR-330 annotations 

    private Context context; 

    NotificationBuilderProvider(Context context) { 
     this.context = context; 
    } 

    @Override 
    public NotificationCompat.Builder get() { 
     return new NotificationCompat.Builder(context); 
    } 

} 

現在你可以寫這樣的包裝:

class NotificationsWrapper { 

    private final NotificationBuilderProvider notificationBuilderProvider; 
    private final NotificationManager notificationManager; 

    NotficiationsWrapper(NotificationBuilderProvider notificationBuilderProvider, NotificationManager notificationManager) { 
     this.notificationBuilderProvider = notificationBuilderProvider; 
     this.notificationManager = notificationManager; 
    } 

    public void postNotification(Map<String, String> data) { 
     Notification notification = notificationBuilderProvider.get() 
      .setContentTitle(data.get("Title")) 
      //etc 
      .build(); 
     notificationManager.notify(ID, notification); 
    } 
} 

現在你可以寫你的NotificationWrapper單元測試是不是測試重量級的服務更容易測試:

//mocks 
NotificationBuilderProvider mockNotificationBuilderProvider; 
NotificationBuilder mockNotificationBuilder; 
NotificationManager mockNotificationManager; 


//system under test 
NotificationWrapper notificationWrapper; 

@Before 
public void setUp() { 
    mockNotificationBuilderProvider = Mockito.mock(NotificationBuilderProvider.class); 
    mockNotificationBuilder = Mockito.mock(NotificationCompat.Builder.class, RETURNS_SELF); 
    mockNotifyManager = Mockito.mock(NotificationManager.class); 
} 

@Test 
public void givenTitleInData_whenPost_thenNotificationHasContentTitle() { 
    //arrange 
    Map<String, String> data = new HashMap<>(); 
    data.put("Title", "MyTitle"); 

    //act 
    notificationsWrapper.postNotification(data); 

    //assert 
    verify(notificationBuilder).setContentTitle(eq("Title")); 
} 

如果這一切看起來有點複雜,最好從簡單的類編寫單元測試開始,直到你熟悉u nit測試和嘲笑。然後你可以移動到測試更復雜的類。該Mockito documentation是一個偉大的地方開始學習嘲笑和單元測試。祝你好運!