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 ” ...

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




Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_加载


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


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_Test_02


例子2:使用Spring Boot Testing工具

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


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_03


@ActiveProfiles

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


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_Test_04


例子1:不使用ActiveProfiles

在没有@ActiveProfiles的时候,外形=默认和没有设定个人资料的豆会被加载到。

源代码ActiveProfileTest:


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_Test_05


例子二:使用ActiveProfiles

当使用了@ActiveProfiles的时候,轮廓匹配的和没有设定个人资料的豆会被加载到。


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_ide_06


总结

在没有@ActiveProfiles的时候,外形=默认和没有设定个人资料的豆会被加载到。

当使用了@ActiveProfiles的时候,轮廓匹配的和没有设定个人资料的豆会被加载到。

@ActiveProfiles同样也可以和@SpringBootTest配合使用,这里就不举例说明了。

Annotations - @JsonTest

@JsonTest是Spring Boot提供的方便测试JSON序列化反序列化的测试工具,在Spring Boot的文档中有一些介绍。

需要注意的是@JsonTest需要Jackson的ObjectMapper,事实上如果你的Spring Boot项目添加了spring-web的Maven依赖,JacksonAutoConfiguration就会自动为你配置一个:


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_Test_07


例子1:简单例子


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot测试_08


例子2: 测试@JsonComponent

@JsonTest可以用来测试@JsonComponent。

这个例子里使用了自定义的@JsonComponent FooJsonComponent:


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_ide_09


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_Test_10


例子3: 使用@ContextConfiguration

事实上@JsonTest也可以配合@ContextConfiguration一起使用。


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_11


@OverrideAutoConfiguration

在 Spring、Spring Boot 和 TestNG 测试指南 ( 1 ) 提到:

除了单元测试(不需要初始化ApplicationContext的测试)外,尽量将测试配置和生产配置保持一致。比如如果生产配置里启用了AutoConfiguration,那么测试配置也应该启用。因为只有这样才能够在测试环境下发现生产环境的问题,也避免出现一些因为配置不同导致的奇怪问题。

那么当我们想在测试代码里关闭Auto Configuration如何处理?

方法1:提供另一套测试配置

方法2:使用@OverrideAutoConfiguration

方法1虽然能够很好的解决问题,但是比较麻烦。而方法2则能够不改变原有配置、不提供新的配置的情况下,就能够关闭Auto Configuration。

在本章节的例子里,我们自己做了一个Auto Configuration类,AutoConfigurationEnableLogger:


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot测试_12


并且在META-INF/spring.factories里注册了它:


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_Test_13


这样一来,只要Spring Boot启动了Auto Configuration就会打印出日志:

2017-08-24 16:44:52.789 INFO 13212 --- [ main] m.c.a.o.AutoConfigurationEnableLogger : Auto Configuration Enabled

例子1:未关闭Auto Configuration


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_14


查看输出的日志,会发现Auto Configuration已经启用。

例子2:关闭Auto Configuration

然后我们用@OverrideAutoConfiguration关闭了Auto Configuration。


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot测试_15


再查看输出的日志,就会发现Auto Configuration已经关闭。

@TestConfiguration

@TestConfiguration是Spring Boot Test提供的一种工具,用它我们可以在一般的@Configuration之外补充测试专门用的Bean或者自定义的配置。

@TestConfiguration实际上是一种@TestComponent,@TestComponent是另一种@Component,在语义上用来指定某个Bean是专门用于测试的。

需要特别注意,你应该使用一切办法避免在生产代码中自动扫描到@TestComponent。 如果你使用@SpringBootApplication启动测试或者生产代码,@TestComponent会自动被排除掉,如果不是则需要像@SpringBootApplication一样添加TypeExcludeFilter:


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_16


例子1:作为内部类

@TestConfiguration和@Configuration不同,它不会阻止@SpringBootTest去查找机制(在Chapter 1: 基本用法 – 使用Spring Boot Testing工具 – 例子4提到过),正如@TestConfiguration的javadoc所说,它只是对既有配置的一个补充。

所以我们在测试代码上添加@SpringBootConfiguration,用@SpringBootTest(classes=…)或者在同package里添加@SpringBootConfiguration类都是可以的。

而且@TestConfiguration作为内部类的时候它是会被@SpringBootTest扫描掉的,这点和@Configuration一样。


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_ide_17


例子2:对@Configuration的补充和覆盖

@TestConfiguration能够:

  1. 补充额外的Bean
  2. 覆盖已存在的Bean

要特别注意第二点,@TestConfiguration能够直接覆盖已存在的Bean,这一点正常的@Configuration是做不到的。

我们先提供了一个正常的@Configuration(Config):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_18


又提供了一个@TestConfiguration,在里面覆盖了foo Bean,并且提供了foo2 Bean(TestConfig):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot测试_19


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_20


再查看输出的日志,就会发现Auto Configuration已经关闭。

例子3:避免@TestConfiguration被扫描到

在上面的这个例子里的TestConfig是会被@ComponentScan扫描到的,如果要避免被扫描到,在本文开头已经提到过了。

先来看一下没有做任何过滤的情形,我们先提供了一个@SpringBootConfiguration(IncludeConfig):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_21


然后有个测试代码引用了它(TestConfigIncludedTest):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_22


从这段代码可以看到TestConfig被加载了。

现在我们使用TypeExcludeFilter来过滤@TestConfiguration(ExcludeConfig1):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_加载_23


再来看看结果(TestConfigExclude_1_Test):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_24


还可以用@SpringBootApplication来排除TestConfig(ExcludeConfig2):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_ide_25


看看结果(TestConfigExclude_2_Test):


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_ide_26


Spring BOOT 运行Test测试用例所需Maven依赖包 springboot testng_springboot 测试_27