[BAEL-1626] testing-modules | A Quick Guide to @TestPropertySource (#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
This commit is contained in:
parent
06f66eda2e
commit
31af57528e
|
@ -0,0 +1,25 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-context-testing</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring.boot.starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<version>${spring.boot.starter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.boot.starter.version>2.0.4.RELEASE</spring.boot.starter.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.testpropertysource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ClassUsingProperty {
|
||||
|
||||
@Value("${baeldung.testpropertysource.one}")
|
||||
private String propertyOne;
|
||||
|
||||
public String retrievePropertyOne() {
|
||||
return propertyOne;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.testpropertysource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ClassUsingProperty.class)
|
||||
@TestPropertySource
|
||||
public class DefaultTestPropertySourceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ClassUsingProperty classUsingProperty;
|
||||
|
||||
@Test
|
||||
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
|
||||
String output = classUsingProperty.retrievePropertyOne();
|
||||
|
||||
assertThat(output).isEqualTo("default-value");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.testpropertysource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ClassUsingProperty.class)
|
||||
@TestPropertySource(locations = "/other-location.properties")
|
||||
public class LocationTestPropertySourceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ClassUsingProperty classUsingProperty;
|
||||
|
||||
@Test
|
||||
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
|
||||
String output = classUsingProperty.retrievePropertyOne();
|
||||
|
||||
assertThat(output).isEqualTo("other-location-value");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.testpropertysource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ClassUsingProperty.class)
|
||||
@TestPropertySource(locations = "/other-location.properties", properties = "baeldung.testpropertysource.one=other-properties-value")
|
||||
public class PropertiesTestPropertySourceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ClassUsingProperty classUsingProperty;
|
||||
|
||||
@Test
|
||||
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
|
||||
String output = classUsingProperty.retrievePropertyOne();
|
||||
|
||||
assertThat(output).isEqualTo("other-properties-value");
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
baeldung.testpropertysource.one=default-value
|
|
@ -0,0 +1 @@
|
|||
baeldung.testpropertysource.one=other-location-value
|
Loading…
Reference in New Issue