BAEL-7136: Improve the companion code for the article to include multiple properties in the @PropertySource annotation (#15885)
This commit is contained in:
parent
cd44988598
commit
a61328e328
@ -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>
|
||||
|
||||
|
@ -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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user