PropertyUtil { public static String getPropertyOrDefault( Class<?> referenceClass, String propertyFile, String key, String defaultValue) { try (InputStream stream = referenceClass.getResourceAsStream(propertyFile)) { if (stream == null) { logger.atSevere().log("Could not load properties file '%s'", propertyFile); return defaultValue; } Properties properties = new Properties(); properties.load(stream); String value = properties.getProperty(key); if (value == null) { logger.atSevere().log("Key %s not found in properties file %s.", key, propertyFile); return defaultValue; } return value; } catch (IOException e) { logger.atSevere().withCause(e).log( "Error while trying to get property value for key %s", key); return defaultValue; } }  static String getPropertyOrDefault(
      Class<?> referenceClass,
      String propertyFile,
      String key,
      String defaultValue);  }
@Test public void defaultValueIsReturnedWhenNoFile() { assertThat( PropertyUtil.getPropertyOrDefault(getClass(), "NonExistentFile", "testKey", "NotFound")) .isEqualTo("NotFound"); }
@Test public void defaultValueIsReturnedWhenKeyNotFound() { assertThat( PropertyUtil.getPropertyOrDefault( getClass(), "test.properties", "testKey2", "NotFound")) .isEqualTo("NotFound"); }
@Test public void valueIsReturnedForFoundKeyAndFile() { assertThat( PropertyUtil.getPropertyOrDefault(getClass(), "test.properties", "testKey", "NotFound")) .isEqualTo("testValue"); }
@Test public void valueWithWhitespaceIsReadProperly() { assertThat( PropertyUtil.getPropertyOrDefault( getClass(), "test.properties", "whitespaceKey", "NotFound")) .isEqualTo("test value with whitespace"); }
@Test public void valueWithEscapedCharactersIsReadUnescaped() { assertThat( PropertyUtil.getPropertyOrDefault( getClass(), "test.properties", "escapedValueKey", "NotFound")) .isEqualTo("http: }
@Test public void keysAfterCommentsAreFound() { assertThat( PropertyUtil.getPropertyOrDefault( getClass(), "test.properties", "postCommentKey", "NotFound")) .isEqualTo("postCommentValue"); }