spring-boot - 如何从 java 测试中排除 META-INF/spring.factories 中的引导
问题描述
在资源/META-INF/spring.factories 中有这一行:
org.springframework.cloud.bootstrap.BootstrapConfiguration=org.path.to.AWSConfig
@RunWith(SpringRunner.class)
运行 AWSConfig 时使用的集成测试执行失败。测试不需要 AWSConfig 即可运行。在排除 AWSConfig 的 SO 上尝试了许多方法,包括:
@TestPropertySource(properties={"spring.autoconfigure.exclude=org.path.to.AWSConfig"})
@EnableAutoConfiguration(exclude = { AWSConfig.class})
- 环境
spring.cloud.config.enabled=false
但 AWSConfig 仍在运行(并且失败)。如何从测试中排除引导配置类?
测试设置:
@RunWith(SpringRunner.class)
@WebMvcTest(UnitLeaderRequestController.class)
@WithMockUser
public class UnitLeaderRequestControllerTest {
@Autowired
MockMvc mvc;
...
}
解决方案
快速解决方案:
将此注释添加到测试类排除了引导:
@TestPropertySource(properties={"spring.cloud.bootstrap.enabled = false"})
替代解决方案:
基于如何在 WebMvcTest 中禁用 Eureka 和 Spring Cloud Config?
我将注释更改为@TestPropertySource(locations = "classpath:application-controller-tests.properties")
,在 /resources 中创建 application-controller-tests.properties 并添加spring.cloud.bootstrap.enabled = false
到文件中,以便多个测试类可以使用 1 个位置的排除项。
进一步的想法
更好的解决方法可能是完全停止使用 META-INF/spring.factories,并使用 bootstrap-{env}.yml 文件。我不确定从项目中删除 META-INF/spring.factories 的影响。
推荐阅读
- wordpress - WordPress 网站上的建议或请求表单,如何自动知道来自哪个用户?
- javascript - Express / HelmetJS / CSP 和 Gzip 资产
- mongodb - MongoDB - 未授权添加副本集成员,但 Mongo Auth 已禁用
- dart - 并排制作两个小部件
- php - 我无法在 PHP 中正确定义变量
- logging - Liferay 7 - 实时日志查看器
- mysql - 使用 JPA 在 MySQL 中大量删除行的最佳实践
- ionic-framework - 将类导入 ionic 3 并在实例化类时出错
- soap - 为什么 SOAP 客户端需要知道 Web 服务的接口?
- c++ - 使用VBO通过加载波前对象绘制立方体