微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mockito如何覆盖测试静态函数的内部if块

如何解决mockito如何覆盖测试静态函数的内部if块

一个静态函数 isNotificationsEnabled ,它在通道 for 循环中有一个 if 块来检查通知通道是否有:

class Utils {

    @NonNull
    static List<NotificationChannel> getNotificationChannels(notificationmanagerCompat nm) {       
        return nm.getNotificationChannels();
    }

    static boolean isNotificationsEnabled(@NonNull Context context) {
        notificationmanagerCompat nm = notificationmanagerCompat.from(context);
        boolean enabled = nm.areNotificationsEnabled();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {          
            if (enabled) {
                List<NotificationChannel> channels = getNotificationChannels(nm);
                boolean someChannelEnabled = channels.isEmpty();
              
                for (NotificationChannel channel : channels) {//need non empty channels in order to run the if block
                    if (channel.getImportance() != notificationmanagerCompat.IMPORTANCE_NONE) {
                        someChannelEnabled = true;
                        break;
                    }
                }
                enabled = enabled && someChannelEnabled;
            }
        } 
        return enabled;
    }
}

尝试了另外一个静态函数getNotificationChannels返回频道列表,并进行了下面的测试,但是不行。

    @Test
    public void test_ isNotificationsEnabled() {
        boolean enabled = notificationmanagerCompat.from(application).areNotificationsEnabled();
        Utils spy = Mockito.spy(new Utils());

        List<NotificationChannel> channelList = new ArrayList<>();
        channelList.add(new NotificationChannel("0","channel_0",notificationmanager.IMPORTANCE_HIGH));
        channelList.add(new NotificationChannel("1","channel_1",notificationmanager.IMPORTANCE_HIGH));

        doReturn(channelList).when(spy).getNotificationChannels(notificationmanagerCompat.from(application));
        boolean b = spy.isNotificationsEnabled(application);
        assertEquals(b,enabled);
        
    }

显然是

doReturn(channelList).when(spy).getNotificationChannels(notificationmanagerCompat.from(application));

不起作用。 如何测试 if 块?

            if (channel.getImportance() != notificationmanagerCompat.IMPORTANCE_NONE) {
                    someChannelEnabled = true;
                    break;
            }

解决方法

更改函数以获取NotificationManager:

static boolean isNotificationsEnabled(@NonNull Context context,NotificationManagerCompat nm) {
        boolean enabled = nm.areNotificationsEnabled();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {          
            if (enabled) {
                List<NotificationChannel> channels = getNotificationChannels(nm);
                boolean someChannelEnabled = channels.isEmpty();
              
                for (NotificationChannel channel : channels) {//need non empty channels in order to run the if block
                    if (channel.getImportance() != NotificationManagerCompat.IMPORTANCE_NONE) {
                        someChannelEnabled = true;
                        break;
                    }
                }
                enabled = enabled && someChannelEnabled;
            }
        } 
        return enabled;
    }

需要将模拟传递到测试中的函数中:

        List<NotificationChannel> channelList = new ArrayList<>();
        channelList.add(new NotificationChannel("0","channel_0",NotificationManager.IMPORTANCE_NONE));
        channelList.add(new NotificationChannel("1","channel_1",NotificationManager.IMPORTANCE_HIGH));

        NotificationManagerCompat nmSpy = Mockito.spy(NotificationManagerCompat.from(application));
        when(nmSpy.getNotificationChannels()).thenReturn(channelList);

        boolean b = Utils.isNotificationsEnabled(application,nmSpy);
        assertEquals(b,enabled);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。