Update tests for spring boot 2.0 compatibility

This commit is contained in:
Spencer Gibb
2017-09-26 16:31:00 -04:00
parent 7a82540f59
commit fee1e87c5a
3 changed files with 21 additions and 10 deletions

View File

@@ -107,7 +107,7 @@ public class ConfigServicePropertySourceLocatorTests {
new ByteArrayInputStream("{}".getBytes()));
this.locator.setRestTemplate(restTemplate);
this.expected.expectCause(IsInstanceOf
.<Throwable> instanceOf(HttpServerErrorException.class));
.instanceOf(IllegalArgumentException.class));
this.expected.expectMessage("fail fast property is set");
this.locator.locate(this.environment);
}
@@ -135,7 +135,7 @@ public class ConfigServicePropertySourceLocatorTests {
Mockito.when(response.getBody()).thenReturn(
new ByteArrayInputStream("".getBytes()));
this.locator.setRestTemplate(restTemplate);
this.expected.expectCause(IsNull.nullValue(Throwable.class));
this.expected.expectCause(IsInstanceOf.instanceOf(IllegalArgumentException.class));
this.expected.expectMessage("fail fast property is set");
this.locator.locate(this.environment);
}

View File

@@ -44,7 +44,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>sample.Application</start-class>
<java.version>1.7</java.version>
<java.version>1.8</java.version>
</properties>
<build>

View File

@@ -1,7 +1,5 @@
package sample;
import java.io.IOException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -15,7 +13,12 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@@ -23,7 +26,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
// Normally spring.cloud.config.enabled:true is the default but since we have the config
// server on the classpath we need to set it explicitly
properties = { "spring.cloud.config.enabled:true",
"management.security.enabled=false" }, webEnvironment = RANDOM_PORT)
"management.security.enabled=false", "endpoints.default.web.enabled=true" }, webEnvironment = RANDOM_PORT)
public class ApplicationTests {
private static int configPort = SocketUtils.findAvailableTcpPort();
@@ -57,10 +60,18 @@ public class ApplicationTests {
}
@Test
@SuppressWarnings("unchecked")
public void contextLoads() {
String foo = new TestRestTemplate()
.getForObject("http://localhost:" + port + "/application/env/info.foo", String.class);
assertEquals("{\"info.foo\":\"bar\"}", foo);
Map res = new TestRestTemplate()
.getForObject("http://localhost:" + port + "/application/env/info.foo", Map.class);
assertThat(res).containsKey("propertySources");
List<Map<String, Object>> sources = (List<Map<String,Object>>) res.get("propertySources");
Optional<Map<String, Object>> properties = sources.stream()
.map(map -> (Map<String, Object>) map.get("properties"))
.filter(map -> map.containsKey("info.foo"))
.map(map -> (Map<String, Object>) map.get("info.foo"))
.findFirst();
assertThat(properties.get()).containsEntry("value", "bar");
}
public static void main(String[] args) throws IOException {