mockito how to coverage test the inner if block of a static function

253 Views Asked by At

Having a static function isNotificationsEnabled which has a if block in the channels for loop to check the notification channel if there is any:

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

tried to have another static function getNotificationChannels to return the channel list, and the following test, but it does not work.

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

apparently the

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

does not work. How to test the if block?

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

There are 1 best solutions below

0
On

change the function to take the NotificatioonManager:

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

need to passing the mock into the function in the test:

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