FallbackConfigurationSource implements ConfigurationSource { @Override public Properties getConfiguration(Environment environment) { boolean allMissEnvironment = true; for (ConfigurationSource source : sources) { try { return source.getConfiguration(environment); } catch (MissingEnvironmentException e) { } catch (IllegalStateException e) { allMissEnvironment = false; } } if (allMissEnvironment) { throw new MissingEnvironmentException(environment.getName()); } throw new IllegalStateException(); } FallbackConfigurationSource(ConfigurationSource... sources); @Override Properties getConfiguration(Environment environment); @Override void init(); @Override String toString();  }
@Test void getConfigurationThrowsWhenAllSourcesThrowOnMissingEnvironment() { makeAllSourcesThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> fallbackConfigurationSource.getConfiguration(mock(Environment.class))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationThrowsWhenAllSourcesThrow() { makeAllSourcesThrow(new IllegalStateException()); assertThatThrownBy(() -> fallbackConfigurationSource.getConfiguration(mock(Environment.class))).isExactlyInstanceOf(IllegalStateException.class); }
@Test void getConfigurationSelectsFirstAvailableConfiguration() { makeAllSourcesThrow(new IllegalStateException()); underlyingSources[LAST_SOURCE_INDEX] = mock(ConfigurationSource.class); when(underlyingSources[LAST_SOURCE_INDEX].getConfiguration(any(Environment.class))).thenReturn(getProps("prop1", "value1")[0]); assertThat(fallbackConfigurationSource.getConfiguration(mock(Environment.class))) .containsOnly(MapEntry.entry("prop1", "value1")); }