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的时候,外形=默认和没有设定个人资料的豆会被加载到。
源代码ActiveProfileTest:
@ContextConfiguration(类 = 配置。类) 公共 类 ActiveProfileTest 延伸 AbstractTestNGSpringContextTests { @Autowired 私人 Foo foo; @Autowired 私人 酒吧 ; @Test public void test(){ 的assertEquals(FOO 。的getName(), “默认”); 的assertEquals(巴。的getName(), “无简档”); } } |
例子二:使用ActiveProfiles
当使用了@ActiveProfiles的时候,轮廓匹配的和没有设定个人资料的豆会被加载到。
源代码ActiveProfileTest:
@ContextConfiguration(类 = 配置。类) [ @ActiveProfiles] [doc-active-profiles](“ product ”) public class ActiveProfileTest extends AbstractTestNGSpringContextTests { @Autowired 私人 Foo foo; @Autowired 私人 酒吧 ; @Test public void test(){ 的assertEquals(FOO 。的getName(), “产品”); 的assertEquals(巴。的getName(), “无简档”); } } |
总结
在没有@ActiveProfiles的时候,外形=默认和没有设定个人资料的豆会被加载到。
当使用了@ActiveProfiles的时候,轮廓匹配的和没有设定个人资料的豆会被加载到。
@ActiveProfiles同样也可以和@SpringBootTest配合使用,这里就不举例说明了。
Annotations - @JsonTest
@JsonTest是Spring Boot提供的方便测试JSON序列化反序列化的测试工具,在Spring Boot的文档中有一些介绍。
需要注意的是@JsonTest需要Jackson的ObjectMapper,事实上如果你的Spring Boot项目添加了spring-web的Maven依赖,JacksonAutoConfiguration就会自动为你配置一个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
这里没有提供关于日期时间的例子,关于这个比较复杂,可以看我的另一篇文章: Spring Boot Jackson对于日期时间类型处理的例子 。
例子1:简单例子
源代码见SimpleJsonTest:
@SpringBootTest(classes = SimpleJsonTest.class) @JsonTest public class SimpleJsonTest extends AbstractTestNGSpringContextTests { @Autowired private JacksonTester<Foo> json; @Test public void testSerialize() throws Exception { Foo details = new Foo("Honda", 12); // 使用通包下的json文件测试结果是否正确 assertThat(this.json.write(details)).isEqualToJson("expected.json"); // 或者使用基于JSON path的校验 assertThat(this.json.write(details)).hasJsonPathStringValue("@.name"); assertThat(this.json.write(details)).extractingJsonPathStringValue("@.name").isEqualTo("Honda"); assertThat(this.json.write(details)).hasJsonPathNumberValue("@.age"); assertThat(this.json.write(details)).extractingJsonPathNumberValue("@.age").isEqualTo(12); } @Test public void testDeserialize() throws Exception { String content = "{\"name\":\"Ford\",\"age\":13}"; Foo actual = this.json.parseObject(content); assertThat(actual).isEqualTo(new Foo("Ford", 13)); assertThat(actual.getName()).isEqualTo("Ford"); assertThat(actual.getAge()).isEqualTo(13); } } |
例子2: 测试@JsonComponent
@JsonTest可以用来测试@JsonComponent。
这个例子里使用了自定义的@JsonComponent FooJsonComponent:
@JsonComponent public class FooJsonComponent { public static class Serializer extends JsonSerializer<Foo> { @Override public void serialize(Foo value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { // ... } } public static class Deserializer extends JsonDeserializer<Foo> { @Override public Foo deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { // ... } } } |