PropertiesLoader { public Properties load() { try { Properties properties = new Properties(); fillPropertiesFromClassPath(properties); fillPropertiesFromFileSystem(properties); if (properties.isEmpty()) { throw new ConfigurationFailedException("Could not find configuration file " + propertiesName + " on either classpath or filesystem."); } return properties; } catch (ConfigurationFailedException e) { throw e; } catch (Exception e) { throw new ConfigurationFailedException(e); } } PropertiesLoader(String propertiesName); Properties load();  }
@Test public void loadsPropertiesFromClassPath() throws Exception { Properties properties = new PropertiesLoader("unittestclasspath.properties").load(); assertThat(properties.getProperty("X"), is("Y")); }
@Test public void prefersPropertiesFromFileSystem() throws Exception { FileUtils.writeStringToFile(fileInWorkingDirectory, "X=X"); Properties properties = new PropertiesLoader("unittestcombination.properties").load(); assertThat(properties.getProperty("X"), is("X")); }
@Test public void addsPropertiesTogether() throws Exception { FileUtils.writeStringToFile(fileInWorkingDirectory, "X=X"); Properties properties = new PropertiesLoader("unittestcombination.properties").load(); assertThat(properties.getProperty("A"), is("B")); }
@Test public void closesStreamOnFileSystemFile() throws Exception { fileInWorkingDirectory.createNewFile(); new PropertiesLoader("unittestcombination.properties").load(); fileInWorkingDirectory.delete(); assertThat(fileInWorkingDirectory.exists(), is(false)); }
@Test(expected = ConfigurationFailedException.class) public void throwsExceptionIfNeitherFileExists() throws Exception { new PropertiesLoader("missing.properties").load(); }