Merge branch 'master' into master
This commit is contained in:
@@ -5,3 +5,4 @@
|
||||
- [Guide to ReflectionTestUtils for Unit Testing](https://www.baeldung.com/spring-reflection-test-utils)
|
||||
- [How to Test the @Scheduled Annotation](https://www.baeldung.com/spring-testing-scheduled-annotation)
|
||||
- [Using SpringJUnit4ClassRunner with Parameterized](https://www.baeldung.com/springjunit4classrunner-parameterized)
|
||||
- [Override properties in Spring]()
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>LATEST</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<ConfigurableApplicationContext> {
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# override properties
|
||||
example.firstProperty=profile
|
||||
@@ -0,0 +1,3 @@
|
||||
# override properties
|
||||
example.firstProperty=file
|
||||
example.secondProperty=file
|
||||
@@ -0,0 +1 @@
|
||||
example.secondProperty=contextFile
|
||||
Reference in New Issue
Block a user