BAEL-7136: Improve the companion code for the article to include multiple properties in the @PropertySource annotation (#15885)

This commit is contained in:
Oscar Mauricio Forero Carrillo
2024-02-16 19:57:57 +01:00
committed by GitHub
parent cd44988598
commit a61328e328
5 changed files with 96 additions and 3 deletions
@@ -8,8 +8,15 @@ public class ClassUsingProperty {
@Value("${baeldung.testpropertysource.one}")
private String propertyOne;
@Value("${baeldung.testpropertysource.two}")
private String propertyTwo;
public String retrievePropertyOne() {
return propertyOne;
}
public String retrievePropertyTwo() {
return propertyTwo;
}
}
@@ -0,0 +1,38 @@
package com.baeldung.testpropertysource;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource(
locations = "/other-location.properties",
properties = {
"baeldung.testpropertysource.one=one",
"baeldung.testpropertysource.two=two",
})
public class MultiplePropertiesInPropertySourceListIntegrationTest {
@Autowired
ClassUsingProperty classUsingProperty;
@Test
public void givenAMultilinePropertySource_whenVariableOneRetrieved_thenValueInPropertyAnnotationIsReturned() {
String output = classUsingProperty.retrievePropertyOne();
assertThat(output).isEqualTo("one");
}
@Test
public void givenAMultilinePropertySource_whenVariableTwoRetrieved_thenValueInPropertyAnnotationIsReturned() {
String output = classUsingProperty.retrievePropertyTwo();
assertThat(output).isEqualTo("two");
}
}
@@ -0,0 +1,38 @@
package com.baeldung.testpropertysource;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource(
locations = "/other-location.properties",
properties = """
baeldung.testpropertysource.one=one
baeldung.testpropertysource.two=two
""")
public class MultiplePropertiesInPropertySourceTextBlockIntegrationTest {
@Autowired
ClassUsingProperty classUsingProperty;
@Test
public void givenAMultilinePropertySource_whenVariableOneRetrieved_thenValueInPropertyAnnotationIsReturned() {
String output = classUsingProperty.retrievePropertyOne();
assertThat(output).isEqualTo("one");
}
@Test
public void givenAMultilinePropertySource_whenVariableTwoRetrieved_thenValueInPropertyAnnotationIsReturned() {
String output = classUsingProperty.retrievePropertyTwo();
assertThat(output).isEqualTo("two");
}
}
@@ -18,7 +18,7 @@ public class PropertiesTestPropertySourceIntegrationTest {
ClassUsingProperty classUsingProperty;
@Test
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
public void givenACustomPropertySource_whenVariableOneRetrieved_thenValueInPropertyAnnotationIsReturned() {
String output = classUsingProperty.retrievePropertyOne();
assertThat(output).isEqualTo("other-properties-value");