【问题标题】:mockito how to coverage test the inner if block of a static functionmockito 如何覆盖测试静态函数的内部 if 块
【发布时间】:2021-05-30 17:11:02
【问题描述】:

有一个静态函数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;
            }

【问题讨论】:

    标签: unit-testing junit mockito test-coverage android-unit-testing


    【解决方案1】:

    更改函数以获取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;
        }
    

    需要将mock传入测试中的函数:

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

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2012-09-21
      • 2010-10-10
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      相关资源
      最近更新 更多