diff --git a/spring-boot-testing/README.MD b/spring-boot-testing/README.MD index 99d7db5743..d303179ef6 100644 --- a/spring-boot-testing/README.MD +++ b/spring-boot-testing/README.MD @@ -6,3 +6,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Testing with Spring and Spock](https://www.baeldung.com/spring-spock-testing) - [Exclude Auto-Configuration Classes in Spring Boot Tests](https://www.baeldung.com/spring-boot-exclude-auto-configuration-test) - [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level) +- [Override properties in Spring] diff --git a/spring-boot-testing/src/main/java/com/baeldung/overrideproperties/Application.java b/spring-boot-testing/src/main/java/com/baeldung/overrideproperties/Application.java new file mode 100644 index 0000000000..ac215bf78e --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/overrideproperties/Application.java @@ -0,0 +1,11 @@ +package com.baeldung.overrideproperties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/overrideproperties/resolver/PropertySourceResolver.java b/spring-boot-testing/src/main/java/com/baeldung/overrideproperties/resolver/PropertySourceResolver.java new file mode 100644 index 0000000000..074092b7df --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/overrideproperties/resolver/PropertySourceResolver.java @@ -0,0 +1,24 @@ +package com.baeldung.overrideproperties.resolver; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class PropertySourceResolver { + + private final String firstProperty; + private final String secondProperty; + + public PropertySourceResolver(@Value("${example.firstProperty}") String firstProperty, @Value("${example.secondProperty}") String secondProperty) { + this.firstProperty = firstProperty; + this.secondProperty = secondProperty; + } + + public String getFirstProperty() { + return firstProperty; + } + + public String getSecondProperty() { + return secondProperty; + } +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/ContextPropertySourceResolverIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/ContextPropertySourceResolverIntegrationTest.java new file mode 100644 index 0000000000..9b692edd7b --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/ContextPropertySourceResolverIntegrationTest.java @@ -0,0 +1,27 @@ +package com.baeldung.overrideproperties; + +import com.baeldung.overrideproperties.resolver.PropertySourceResolver; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@ContextConfiguration(initializers = PropertyOverrideContextInitializer.class, classes = Application.class) +public class ContextPropertySourceResolverIntegrationTest { + + @Autowired private PropertySourceResolver propertySourceResolver; + + @Test + public void shouldContext_overridePropertyValues() { + final String firstProperty = propertySourceResolver.getFirstProperty(); + final String secondProperty = propertySourceResolver.getSecondProperty(); + + assertEquals(PropertyOverrideContextInitializer.PROPERTY_FIRST_VALUE, firstProperty); + assertEquals("contextFile", secondProperty); + } + +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/ProfilePropertySourceResolverIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/ProfilePropertySourceResolverIntegrationTest.java new file mode 100644 index 0000000000..95d83420b7 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/ProfilePropertySourceResolverIntegrationTest.java @@ -0,0 +1,29 @@ +package com.baeldung.overrideproperties; + +import com.baeldung.overrideproperties.resolver.PropertySourceResolver; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles("test") +public class ProfilePropertySourceResolverIntegrationTest { + + @Autowired private PropertySourceResolver propertySourceResolver; + + @Test + public void shouldProfiledProperty_overridePropertyValues() { + final String firstProperty = propertySourceResolver.getFirstProperty(); + final String secondProperty = propertySourceResolver.getSecondProperty(); + + assertEquals("profile", firstProperty); + assertEquals("file", secondProperty); + } + +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/PropertyOverrideContextInitializer.java b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/PropertyOverrideContextInitializer.java new file mode 100644 index 0000000000..a8c4267c5c --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/PropertyOverrideContextInitializer.java @@ -0,0 +1,17 @@ +package com.baeldung.overrideproperties; + +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.test.context.support.TestPropertySourceUtils; + +public class PropertyOverrideContextInitializer implements ApplicationContextInitializer { + + static final String PROPERTY_FIRST_VALUE = "contextClass"; + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(configurableApplicationContext, "example.firstProperty=" + PROPERTY_FIRST_VALUE); + + TestPropertySourceUtils.addPropertiesFilesToEnvironment(configurableApplicationContext, "context-override-application.properties"); + } +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/SpringBootPropertySourceResolverIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/SpringBootPropertySourceResolverIntegrationTest.java new file mode 100644 index 0000000000..573a46dd5f --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/SpringBootPropertySourceResolverIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.overrideproperties; + +import com.baeldung.overrideproperties.resolver.PropertySourceResolver; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(properties = { "example.firstProperty=annotation" }) +public class SpringBootPropertySourceResolverIntegrationTest { + + @Autowired private PropertySourceResolver propertySourceResolver; + + @Test + public void shouldSpringBootTestAnnotation_overridePropertyValues() { + final String firstProperty = propertySourceResolver.getFirstProperty(); + final String secondProperty = propertySourceResolver.getSecondProperty(); + + Assert.assertEquals("annotation", firstProperty); + Assert.assertEquals("file", secondProperty); + } + +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/TestResourcePropertySourceResolverIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/TestResourcePropertySourceResolverIntegrationTest.java new file mode 100644 index 0000000000..c724713854 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/overrideproperties/TestResourcePropertySourceResolverIntegrationTest.java @@ -0,0 +1,27 @@ +package com.baeldung.overrideproperties; + +import com.baeldung.overrideproperties.resolver.PropertySourceResolver; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class TestResourcePropertySourceResolverIntegrationTest { + + @Autowired private PropertySourceResolver propertySourceResolver; + + @Test + public void shouldTestResourceFile_overridePropertyValues() { + final String firstProperty = propertySourceResolver.getFirstProperty(); + final String secondProperty = propertySourceResolver.getSecondProperty(); + + assertEquals("file", firstProperty); + assertEquals("file", secondProperty); + } + +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application-test.properties b/spring-boot-testing/src/test/resources/application-test.properties new file mode 100644 index 0000000000..da187d8086 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-test.properties @@ -0,0 +1,5 @@ +# test properties +spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration + +# override properties +example.firstProperty=profile \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application.properties b/spring-boot-testing/src/test/resources/application.properties new file mode 100644 index 0000000000..e71b24a4e1 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application.properties @@ -0,0 +1,7 @@ +# security +spring.security.user.name=john +spring.security.user.password=123 + +# override properties +example.firstProperty=file +example.secondProperty=file \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/context-override-application.properties b/spring-boot-testing/src/test/resources/context-override-application.properties new file mode 100644 index 0000000000..c39e5d3966 --- /dev/null +++ b/spring-boot-testing/src/test/resources/context-override-application.properties @@ -0,0 +1 @@ +example.secondProperty=contextFile \ No newline at end of file