Spring、Spring Boot和TestNG測試指南 - @TestPropertySource 原

碼雲地址

@TestPropertySource可以用來覆蓋掉來自於系統環境變量、Java系統屬性、@PropertySource的屬性。

同時@TestPropertySource(properties=...)優先級高於@TestPropertySource(locations=...)

利用它我們可以很方便的在測試代碼裏微調、模擬配置(比如修改操作系統目錄分隔符、數據源等)。

例子1: 使用Spring Testing工具

我們先使用@PropertySource將一個外部properties文件加載進來,PropertySourceConfig

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

然後我們用@TestPropertySource覆蓋了這個property:

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

最後我們測試了是否覆蓋成功(結果是成功的):

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

同時我們還對@TestPropertySource做了一些其他的測試,具體情況你可以自己觀察。爲了方便你觀察@TestPropertySource對系統環境變量和Java系統屬性的覆蓋效果,我們在一開始打印出了它們的值。

源代碼TestPropertyTest

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

  private Environment environment;

  @Override
  public void setEnvironment(Environment environment) {
    this.environment = environment;
    Map<String, Object> systemEnvironment = ((ConfigurableEnvironment) environment).getSystemEnvironment();
    System.out.println("=== System Environment ===");
    System.out.println(getMapString(systemEnvironment));
    System.out.println();

    System.out.println("=== Java System Properties ===");
    Map<String, Object> systemProperties = ((ConfigurableEnvironment) environment).getSystemProperties();
    System.out.println(getMapString(systemProperties));
  }

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

  @Test
  public void testOverrideSystemEnvironment() {
    assertEquals(environment.getProperty("PATH"), "aaa");
  }

  @Test
  public void testOverrideJavaSystemProperties() {
    assertEquals(environment.getProperty("java.runtime.name"), "bbb");
  }

  @Test
  public void testInlineTestPropertyOverrideResourceLocationTestProperty() {
    assertEquals(environment.getProperty("bar"), "uvw");
  }

  private String getMapString(Map<String, Object> map) {
    return String.join("\n",
        map.keySet().stream().map(k -> k + "=" + map.get(k)).collect(toList())
    );
  }
}

例子2: 使用Spring Boot Testing工具

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

源代碼見TestPropertyTest

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

參考文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章