Skip to content

Commit 31af575

Browse files
rozagerardomaibin
authored andcommitted
[BAEL-1626] testing-modules | A Quick Guide to @TestPropertySource (eugenp#5255)
* *added tests using testpropertysource * fix: using property for version in pom file * fix, added dependency with compile scope to build successfully on mvn clean install
1 parent 06f66ed commit 31af575

7 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-context-testing</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter</artifactId>
12+
<version>${spring.boot.starter.version}</version>
13+
</dependency>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-test</artifactId>
17+
<scope>test</scope>
18+
<version>${spring.boot.starter.version}</version>
19+
</dependency>
20+
</dependencies>
21+
22+
<properties>
23+
<spring.boot.starter.version>2.0.4.RELEASE</spring.boot.starter.version>
24+
</properties>
25+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.testpropertysource;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class ClassUsingProperty {
8+
9+
@Value("${baeldung.testpropertysource.one}")
10+
private String propertyOne;
11+
12+
public String retrievePropertyOne() {
13+
return propertyOne;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.testpropertysource;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.TestPropertySource;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
@RunWith(SpringRunner.class)
13+
@ContextConfiguration(classes = ClassUsingProperty.class)
14+
@TestPropertySource
15+
public class DefaultTestPropertySourceIntegrationTest {
16+
17+
@Autowired
18+
ClassUsingProperty classUsingProperty;
19+
20+
@Test
21+
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
22+
String output = classUsingProperty.retrievePropertyOne();
23+
24+
assertThat(output).isEqualTo("default-value");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.testpropertysource;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.TestPropertySource;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
@RunWith(SpringRunner.class)
13+
@ContextConfiguration(classes = ClassUsingProperty.class)
14+
@TestPropertySource(locations = "/other-location.properties")
15+
public class LocationTestPropertySourceIntegrationTest {
16+
17+
@Autowired
18+
ClassUsingProperty classUsingProperty;
19+
20+
@Test
21+
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
22+
String output = classUsingProperty.retrievePropertyOne();
23+
24+
assertThat(output).isEqualTo("other-location-value");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.testpropertysource;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.TestPropertySource;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
@RunWith(SpringRunner.class)
13+
@ContextConfiguration(classes = ClassUsingProperty.class)
14+
@TestPropertySource(locations = "/other-location.properties", properties = "baeldung.testpropertysource.one=other-properties-value")
15+
public class PropertiesTestPropertySourceIntegrationTest {
16+
17+
@Autowired
18+
ClassUsingProperty classUsingProperty;
19+
20+
@Test
21+
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
22+
String output = classUsingProperty.retrievePropertyOne();
23+
24+
assertThat(output).isEqualTo("other-properties-value");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baeldung.testpropertysource.one=default-value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baeldung.testpropertysource.one=other-location-value

0 commit comments

Comments
 (0)