【问题标题】:How to make android unit test for notifications?如何对通知进行android单元测试?
【发布时间】:2017-06-02 17:29:07
【问题描述】:

我有课程 handleFirebaseMessages 。它包含函数 onMessageReceived() 。该函数负责接收数据和创建通知。 onMessageReceived() 接收 RemoteMessages 对象中的数据。我正在尝试为此功能编写测试。但我还没有完全明白。

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?
}

【问题讨论】:

    标签: android unit-testing mockito android-notifications


    【解决方案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"));
    }
    

    如果这一切看起来有点复杂,最好先为简单的类编写单元测试,直到您熟悉单元测试和模拟的概念。然后您可以继续测试更复杂的类。 Mockito documentation 是开始学习模拟和单元测试的好地方。祝你好运!

    【讨论】:

    • 只是一个观察。 notificationManager.notify(ID, notification)中提到的ID应该在NotificationsWrapper的构建参数中接收或者是这个类的静态属性。
    • 另外,我想你忘了声明类似when(mockNotificationBuilderProvider.get()).thenReturn(mockNotificationBuilder)
    【解决方案2】:

    现在在 onMessageReceived(message) 中,创建一个自定义通知。 试试这个链接Custom notificatoin

    希望这会有所帮助。

    【讨论】:

    • 不是设计问题,而是单元测试
    猜你喜欢
    • 2020-09-25
    • 2022-11-17
    • 2012-01-08
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多