@SpringApplicationConfiguration
和@ContextConfiguration
在JUnit测试用例中有何不同?
@SpringApplicationConfiguration
和@ContextConfiguration
在JUnit测试用例中有何不同?
@ContextConfiguration
是 Spring Test Framework 的一个注解,适用于所有的 Spring 应用程序,@SpringApplicationConfiguration
是来自 Spring Boot 的一个复合注解,其中包含 ContextConfiguration
和自定义的 SpringApplicationContextLoader
作为加载器。
@ContextConfiguration
和@SpringApplicationConfiguration
都是为了集成测试而加载和配置ApplicationContext,但@ContextConfiguration
在支持方面存在一些缺陷。
在Spring 3.1
之前,只支持基于路径的资源位置(通常是XML配置文件)。从Spring 3.1开始,上下文加载器可以选择支持基于路径或基于类的资源。
从Spring 4.0.4开始,上下文加载器可以同时支持基于路径和基于类的资源。因此,@ContextConfiguration
可用于声明基于路径的资源位置(通过locations()或value()属性)或注释类(通过classes()属性)。
然而,请注意,大多数SmartContextLoader的实现仅支持单个资源类型。从Spring 4.1开始,基于路径的资源位置可以是XML配置文件或Groovy脚本(如果Groovy在类路径上)。当然,第三方框架可以选择支持其他类型的基于路径的资源。
@SpringApplicationConfiguration
与标准的@ContextConfiguration
类似,但使用Spring Boot的SpringApplicationContextLoader。