FoamMain { @SuppressWarnings("unchecked") <T extends Service> List<T> getEnabledServicesForType(Class<T> clazz) { List<T> servicesOfType = new ArrayList<>(); if(services != null && !services.isEmpty()) { for (Service service : services.values()) { if (clazz.isAssignableFrom(service.getClass())) { if (service.isEnabled()) { servicesOfType.add((T) service); } } } } return servicesOfType; } FoamMain(Context context);   }
@Test public void testGetEnabledServicesForType(){ FoamMain foamMain = new FoamMain(null); List response = foamMain.getEnabledServicesForType(EventTrackingService.class); assertNotNull(response); assertTrue(response.isEmpty()); EventTrackingService eventTrackingService1 = mock(EventTrackingService.class); EventTrackingService eventTrackingService2 = mock(EventTrackingService.class); EventTrackingService eventTrackingService3 = mock(EventTrackingService.class); EventTrackingService eventTrackingService4 = mock(EventTrackingService.class); when(eventTrackingService1.isEnabled()).thenReturn(true); when(eventTrackingService2.isEnabled()).thenReturn(true); when(eventTrackingService3.isEnabled()).thenReturn(false); when(eventTrackingService4.isEnabled()).thenReturn(true); foamMain.services.put(ServiceType.PAGERDUTY, eventTrackingService1); foamMain.services.put(ServiceType.HOCKEYAPP, eventTrackingService2); foamMain.services.put(ServiceType.PAPERTRAIL, eventTrackingService3); foamMain.services.put(ServiceType.MIXPANEL, eventTrackingService4); CrashReportingService crashReportingService1 = mock(CrashReportingService.class); CrashReportingService crashReportingService2 = mock(CrashReportingService.class); when(crashReportingService1.isEnabled()).thenReturn(true); when(crashReportingService2.isEnabled()).thenReturn(true); foamMain.services.put(ServiceType.FLURRY, crashReportingService1); foamMain.services.put(ServiceType.LOGENTRIES, crashReportingService2); LoggingService loggingService1 = mock(LoggingService.class); when(loggingService1.isEnabled()).thenReturn(true); foamMain.services.put(ServiceType.GOOGLE_ANALYTICS, loggingService1); response = foamMain.getEnabledServicesForType(EventTrackingService.class); assertNotNull(response); assertEquals(3, response.size()); response = foamMain.getEnabledServicesForType(CrashReportingService.class); assertNotNull(response); assertEquals(2, response.size()); response = foamMain.getEnabledServicesForType(LoggingService.class); assertNotNull(response); assertEquals(1, response.size()); }