如何对通知进行android单元测试? [英] How to make android unit test for notifications?

查看:186
本文介绍了如何对通知进行android单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类handleFirebaseMessages.它包含功能onMessageReceived().该功能负责接收数据和创建通知. onMessageReceived()RemoteMessages对象中接收数据.我正在尝试为此功能编写测试.但是我还没有完全了解它.

I have the class handleFirebaseMessages . It contains the function onMessageReceived() . This function is in charge of receiving data and creating notifications. onMessageReceived() receives data in a RemoteMessages object. I am trying to write a test for this function. But I've not got it completely.

处理FirebaseMessages

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

我已经能够编写部分测试代码.我该如何完成呢?

I've been able to write a part of the test code. How do I complete this?

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

推荐答案

您可以通过包装Notifications API使其可测试来对其进行测试.首先,您需要一种将模拟的依赖项传递给包装器的构造函数的方法.您可以这样写:

You test this by wrapping the Notifications API to make it testable. First you will need a way of passing in mocked dependencies to the constructor of your wrapper. You can write something like this:

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);
    }

}

现在您可以像这样编写包装器了:

Now you can write a wrapper like this:

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编写单元测试了,比测试重量级服务更容易进行测试:

Now you can write a unit test for your NotificationWrapper which is easier to testing than testing the heavyweight service:

//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文档是一个开始学习模拟和单元测试的好地方.祝你好运!

If this all seems a little complicated, it's better to start with writing unit tests for simple classes until you are comfortable with the concepts of unit testing and mocking. Then you can move up to testing more complex classes. The Mockito documentation is a great place to start to learn about mocking and unit testing. Good luck!

这篇关于如何对通知进行android单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆