设为首页 加入收藏

TOP

Spring、Spring Boot 和 TestNG 测试指南 ( 3 )(一)
2017-12-11 09:19:05 】 浏览:680
Tags:Spring Boot TestNG 测试 指南

Spring&Spring Boot Testing工具提供了一些方便测试的Annotation,本文会对其中的一些做一些讲解。

@TestPropertySource

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

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

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

例子1:使用Spring Testing工具

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

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

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

TestPropertySource(properties  = { “ foo = xyz ”  ...

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

@Test 
public void testOverridePropertySource(){
的assertEquals(环境。的getProperty( “ FOO ”), “ XYZ ”);
}

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

源代码TestPropertyTest:

@ContextConfiguration(类 =  PropertySourceConfig 。类)
 @TestPropertySource(
     属性 = { “富= XYZ ”,“巴= UVW ”,“ PATH = AAA ”,“ java.runtime.name = BBB ” },
     位置 =  “类路径:我/chanjar/annotation/testps/ex1/test-property-source.properties “
)
公共 类 TestPropertyTest  扩展 AbstractTestNGSpringContextTests  实现 EnvironmentAware {

  私人 环境环境;

  @覆盖
  公共 无效 setEnvironment(环境 环境){
     此。环境=环境;
    Map < String,Object > systemEnvironment =((ConfigurableEnvironment)环境)。getSystemEnvironment();
    系统。出去。println(“ ===系统环境=== ”);
    系统。出去。的println(getMapString(systemEnvironment));
    系统。出去。的println();

    系统。出去。println(“ === Java系统属性=== ”);
    Map < String,Object > systemProperties =((ConfigurableEnvironment)环境)。getSystemProperties();
    系统。出去。的println(getMapString(systemProperties));
  }

  @Test 
  public  void  testOverridePropertySource(){
    的assertEquals(环境。的getProperty( “ FOO ”), “ XYZ ”);
  }

  @Test 
  public  void  testOverrideSystemEnvironment(){
    的assertEquals(环境。的getProperty( “ PATH ”), “ AAA ”);
  }

  @Test 
  public  void  testOverrideJavaSystemProperties(){
    的assertEquals(环境。的getProperty( “ java.runtime.name ”), “ BBB ”);
  }

  @Test 
  public  void  testInlineTestPropertyOverrideResourceLocationTestProperty(){
    的assertEquals(环境。的getProperty( “条”), “ UVW ”);
  }

  private  String  getMapString(Map < String,Object >  map){
     return  String 。加入(“ \ n ”,
        地图。keySet()。stream()。地图(K - > ? +  “ = ”  +地图。得到(k))的。收集(toList())
    );
  }
}

例子2:使用Spring Boot Testing工具

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

源代码见TestPropertyTest:

@SpringBootTest(类 =  PropertySourceConfig 。类)
 @TestPropertySource(
     属性 = { “富= XYZ ”,“巴= UVW ”,“ PATH = AAA ”,“ java.runtime.name = BBB ” },
     位置 =  “类路径:我/chanjar/annotation/testps/ex1/test-property-source.properties “
)
公共 类 TestPropertyTest  扩展 AbstractTestNGSpringContextTests  实现 EnvironmentAware {
   // ..

@ActiveProfiles

@ActiveProfiles可以用来在测试的时候启用某些资料的豆本章节的测试代码使用了下面的这个配置:

@Configuration 
public  class  Config {

  @Bean 
  @Profile(“ dev ”)
   public  Foo  fooDev(){
     return  new  Foo(“ dev ”);
  }

  @Bean 
  @Profile(“ product ”)
   public  Foo  fooProduct(){
     return  new  Foo(“ product ”);
  }

  @Bean 
  @Profile(“ default ”)
   public  Foo  fooDefault(){
     return  new  Foo(“ default ”);
  }

  @Bean 
  public  bar  bar(){
     return  new  bar(“ no profile ”);
  }

}

例子1:不使用ActiveProfiles

在没有@Acti

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring、Spring Boot 和 TestNG .. 下一篇一道题看清动态规划的前世今生 ( ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目