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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 96 additions and 3 deletions

View File

@ -75,6 +75,16 @@
<build>
<finalName>spring-testing</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
@ -87,7 +97,7 @@
<!-- testing -->
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
<awaitility.version>3.1.6</awaitility.version>
<spring.version>5.3.4</spring.version>
<spring.version>6.1.3</spring.version>
<javax.persistence.version>2.1.1</javax.persistence.version>
</properties>

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");