BAEL-3741: Add usage examples of @TestPropertySource (#8493)
This commit is contained in:
parent
0e6fd4f393
commit
7a186e9bc8
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.properties.testproperty;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
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)
|
||||||
|
@TestPropertySource("/foo.properties")
|
||||||
|
public class FilePropertyInjectionUnitTest {
|
||||||
|
|
||||||
|
@Value("${foo}")
|
||||||
|
private String foo;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFilePropertyProvided_thenProperlyInjected() {
|
||||||
|
assertThat(foo).isEqualTo("bar");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.properties.testproperty;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
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)
|
||||||
|
@TestPropertySource(properties = {"foo=bar"})
|
||||||
|
public class PropertyInjectionUnitTest {
|
||||||
|
|
||||||
|
@Value("${foo}")
|
||||||
|
private String foo;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPropertyProvided_thenProperlyInjected() {
|
||||||
|
assertThat(foo).isEqualTo("bar");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.properties.testproperty;
|
||||||
|
|
||||||
|
import com.baeldung.properties.reloading.SpringBootPropertiesTestApplication;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class)
|
||||||
|
public class SpringBootPropertyInjectionIntegrationTest {
|
||||||
|
|
||||||
|
@Value("${foo}")
|
||||||
|
private String foo;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringBootPropertyProvided_thenProperlyInjected() {
|
||||||
|
assertThat(foo).isEqualTo("bar");
|
||||||
|
}
|
||||||
|
}
|
1
spring-boot-properties/src/test/resources/foo.properties
Normal file
1
spring-boot-properties/src/test/resources/foo.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
foo=bar
|
Loading…
x
Reference in New Issue
Block a user