51Testing软件测试论坛

标题: Spring、Spring Boot和TestNG测试指南 [打印本页]

作者: 测试积点老人    时间: 2018-12-26 09:26
标题: Spring、Spring Boot和TestNG测试指南
例子1: 使用Spring Testing工具

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

  1. @Configuration
  2. @PropertySource("classpath:me/chanjar/annotation/testps/ex1/property-source.properties")
  3. public class PropertySourceConfig {
  4. }
  5. file: property-source.properties
  6. 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. }
复制代码






欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2