CacheServiceLoader { public static void initialise(final Properties properties) { if (null == properties) { LOGGER.warn("received null properties - exiting initialise method without creating service"); return; } final String cacheClass = properties.getProperty(CacheProperties.CACHE_SERVICE_CLASS); if (null == cacheClass) { if (null == service) { LOGGER.debug("No cache service class was specified in properties."); } return; } try { service = Class.forName(cacheClass).asSubclass(ICacheService.class).newInstance(); } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new IllegalArgumentException("Failed to instantiate cache using class " + cacheClass, e); } service.initialise(properties); if (!shutdownHookAdded) { Runtime.getRuntime().addShutdownHook(new Thread(CacheServiceLoader::shutdown)); shutdownHookAdded = true; } } private  CacheServiceLoader(); static void initialise(final Properties properties); static ICacheService getService(); static boolean isEnabled(); static void shutdown();  }
@DisplayName("Should not throw NullPointer when Loader is initialised with null properties") @Test public void shouldINotThrowNullPointerExceptionOnInitialiseLoader() { CacheServiceLoader.initialise(null); }
@Test public void shouldThrowAnExceptionWhenSystemVariableIsInvalid() { final String invalidClassName = "invalid.cache.name"; serviceLoaderProperties.setProperty(CacheProperties.CACHE_SERVICE_CLASS, invalidClassName); final Exception exception = assertThrows(IllegalArgumentException.class, () -> { CacheServiceLoader.initialise(serviceLoaderProperties); }); final String expected = "Failed to instantiate cache using class invalid.cache.name"; assertEquals(expected, exception.getMessage()); }