diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 42b58978..0f1b9684 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -62,7 +62,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator CompositePropertySource composite = new CompositePropertySource("configService"); RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(client) : this.restTemplate; - RuntimeException error = null; + Exception error = null; String errorBody = null; try { Environment result = restTemplate.exchange( @@ -78,17 +78,18 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator } catch (HttpServerErrorException e) { error = e; - if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) { + if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders() + .getContentType())) { errorBody = e.getResponseBodyAsString(); } } catch (Exception e) { - error = new IllegalStateException( - "Could not locate PropertySource. The fail fast property is set, failing", - e); + error = e; } if (client != null && client.isFailFast()) { - throw error; + throw new IllegalStateException( + "Could not locate PropertySource and the fail fast property is set, failing", + error); } logger.error("Could not locate PropertySource: " + (errorBody == null ? error.getMessage() : errorBody)); diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java index 5c8c3990..d77530c9 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertNull; import java.io.ByteArrayInputStream; import java.net.URI; +import org.hamcrest.core.IsInstanceOf; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -75,8 +76,8 @@ public class ConfigServicePropertySourceLocatorTests { Mockito.when(response.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR); Mockito.when(response.getBody()).thenReturn(new ByteArrayInputStream("{}".getBytes())); locator.setRestTemplate(restTemplate); - expected.expect(HttpServerErrorException.class); - expected.expectMessage("500"); + expected.expectCause(IsInstanceOf.instanceOf(HttpServerErrorException.class)); + expected.expectMessage("fail fast property is set"); assertNull(locator.locate(environment)); }