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
在没有@ActiveProfiles的时候,外形=默认和没有设定小我私家资料的豆会被加载到。