2017-06-02 54 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是一个伟大的地方开始学习嘲笑和单元测试。祝你好运!