Chapter 2: Annotations - @TestPropertySource

@TestPropertySource可以用来覆盖掉来自于系统环境变量、Java系统属性、@PropertySource的属性。

同时@TestPropertySource(properties=...)优先级高于@TestPropertySource(locations=...)

利用它我们可以很方便的在测试代码里微调、模拟配置(比如修改操作系统目录分隔符、数据源等)。

例子1: 使用Spring Testing工具

我们先使用@PropertySource将一个外部properties文件加载进来,PropertySourceConfig

  1. @Configuration
  2. @PropertySource("classpath:me/chanjar/annotation/testps/ex1/property-source.properties")
  3. public class PropertySourceConfig {
  4. }
  1. file: property-source.properties
  2. foo=abc

然后我们用@TestPropertySource覆盖了这个property:

  1. @TestPropertySource(properties = { "foo=xyz" ...

最后我们测试了是否覆盖成功(结果是成功的):

  1. @Test
  2. public void testOverridePropertySource() {
  3. assertEquals(environment.getProperty("foo"), "xyz");
  4. }

同时我们还对@TestPropertySource做了一些其他的测试,具体情况你可以自己观察。为了方便你观察@TestPropertySource对系统环境变量和Java系统属性的覆盖效果,我们在一开始打印出了它们的值。

源代码TestPropertyTest

  1. @ContextConfiguration(classes = PropertySourceConfig.class)
  2. @TestPropertySource(
  3. properties = { "foo=xyz", "bar=uvw", "PATH=aaa", "java.runtime.name=bbb" },
  4. locations = "classpath:me/chanjar/annotation/testps/ex1/test-property-source.properties"
  5. )
  6. public class TestPropertyTest extends AbstractTestNGSpringContextTests implements EnvironmentAware {
  7. private Environment environment;
  8. @Override
  9. public void setEnvironment(Environment environment) {
  10. this.environment = environment;
  11. Map<String, Object> systemEnvironment = ((ConfigurableEnvironment) environment).getSystemEnvironment();
  12. System.out.println("=== System Environment ===");
  13. System.out.println(getMapString(systemEnvironment));
  14. System.out.println();
  15. System.out.println("=== Java System Properties ===");
  16. Map<String, Object> systemProperties = ((ConfigurableEnvironment) environment).getSystemProperties();
  17. System.out.println(getMapString(systemProperties));
  18. }
  19. @Test
  20. public void testOverridePropertySource() {
  21. assertEquals(environment.getProperty("foo"), "xyz");
  22. }
  23. @Test
  24. public void testOverrideSystemEnvironment() {
  25. assertEquals(environment.getProperty("PATH"), "aaa");
  26. }
  27. @Test
  28. public void testOverrideJavaSystemProperties() {
  29. assertEquals(environment.getProperty("java.runtime.name"), "bbb");
  30. }
  31. @Test
  32. public void testInlineTestPropertyOverrideResourceLocationTestProperty() {
  33. assertEquals(environment.getProperty("bar"), "uvw");
  34. }
  35. private String getMapString(Map<String, Object> map) {
  36. return String.join("\n",
  37. map.keySet().stream().map(k -> k + "=" + map.get(k)).collect(toList())
  38. );
  39. }
  40. }

例子2: 使用Spring Boot Testing工具

@TestPropertySource也可以和@SpringBootTest一起使用。

源代码见TestPropertyTest

  1. @SpringBootTest(classes = PropertySourceConfig.class)
  2. @TestPropertySource(
  3. properties = { "foo=xyz", "bar=uvw", "PATH=aaa", "java.runtime.name=bbb" },
  4. locations = "classpath:me/chanjar/annotation/testps/ex1/test-property-source.properties"
  5. )
  6. public class TestPropertyTest extends AbstractTestNGSpringContextTests implements EnvironmentAware {
  7. // ...
  8. }

参考文档