diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java index 9309c9a1..a7e6975e 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java @@ -1,14 +1,11 @@ package org.springframework.cloud.context.refresh; -import static org.assertj.core.api.Assertions.assertThat; - import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.After; import org.junit.Test; import org.mockito.Mockito; import org.springframework.boot.SpringApplication; @@ -22,53 +19,49 @@ import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; +import static org.assertj.core.api.Assertions.assertThat; + public class ContextRefresherTests { private RefreshScope scope = Mockito.mock(RefreshScope.class); - private ConfigurableApplicationContext context; - - @After - public void close() { - if (context != null) { - context.close(); - } - } @Test public void orderNewPropertiesConsistentWithNewContext() { - context = SpringApplication.run(ContextRefresherTests.class, + try (ConfigurableApplicationContext context = SpringApplication.run(ContextRefresherTests.class, "--spring.main.webEnvironment=false", "--debug=false", - "--spring.main.bannerMode=OFF"); - context.getEnvironment().setActiveProfiles("refresh"); - List names = names(context.getEnvironment().getPropertySources()); - assertThat(names).doesNotContain( - "applicationConfig: [classpath:/bootstrap-refresh.properties]"); - ContextRefresher refresher = new ContextRefresher(context, scope); - refresher.refresh(); - names = names(context.getEnvironment().getPropertySources()); - assertThat(names) - .contains("applicationConfig: [classpath:/bootstrap-refresh.properties]"); - assertThat(names).containsSequence( - "applicationConfig: [classpath:/application.properties]", - "applicationConfig: [classpath:/bootstrap-refresh.properties]"); + "--spring.main.bannerMode=OFF")) { + context.getEnvironment().setActiveProfiles("refresh"); + List names = names(context.getEnvironment().getPropertySources()); + assertThat(names).doesNotContain( + "applicationConfig: [classpath:/bootstrap-refresh.properties]"); + ContextRefresher refresher = new ContextRefresher(context, scope); + refresher.refresh(); + names = names(context.getEnvironment().getPropertySources()); + assertThat(names) + .contains("applicationConfig: [classpath:/bootstrap-refresh.properties]"); + assertThat(names).containsSequence( + "applicationConfig: [classpath:/application.properties]", + "applicationConfig: [classpath:/bootstrap-refresh.properties]"); + } } @Test public void bootstrapPropertySourceAlwaysFirst() { // Use spring.cloud.bootstrap.name to switch off the defaults (which would pick up // a bootstrapProperties immediately - context = SpringApplication.run(ContextRefresherTests.class, + try (ConfigurableApplicationContext context = SpringApplication.run(ContextRefresherTests.class, "--spring.main.webEnvironment=false", "--debug=false", - "--spring.main.bannerMode=OFF", "--spring.cloud.bootstrap.name=refresh"); - List names = names(context.getEnvironment().getPropertySources()); - assertThat(names).doesNotContain("bootstrapProperties"); - ContextRefresher refresher = new ContextRefresher(context, scope); - TestPropertyValues.of( - "spring.cloud.bootstrap.sources: org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration") - .applyTo(context); - refresher.refresh(); - names = names(context.getEnvironment().getPropertySources()); - assertThat(names).first().isEqualTo("bootstrapProperties"); + "--spring.main.bannerMode=OFF", "--spring.cloud.bootstrap.name=refresh")) { + List names = names(context.getEnvironment().getPropertySources()); + assertThat(names).doesNotContain("bootstrapProperties"); + ContextRefresher refresher = new ContextRefresher(context, scope); + TestPropertyValues.of( + "spring.cloud.bootstrap.sources: org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration") + .applyTo(context); + refresher.refresh(); + names = names(context.getEnvironment().getPropertySources()); + assertThat(names).first().isEqualTo("bootstrapProperties"); + } } private List names(MutablePropertySources propertySources) { @@ -83,8 +76,8 @@ public class ContextRefresherTests { // This is added to bootstrap context as a source in bootstrap.properties protected static class PropertySourceConfiguration implements PropertySourceLocator { - public static Map MAP = new HashMap( - Collections.singletonMap("bootstrap.foo", "refresh")); + public static Map MAP = new HashMap<>( + Collections.singletonMap("bootstrap.context-refresh.foo", "refresh")); @Override public PropertySource locate(Environment environment) {