java - 运行测试时如何指示 Spring Boot 使用测试资源中的 application.properties 文件
问题描述
我有以下 Spring Boot 应用程序的项目结构。
问题是,每当我运行测试时,Spring Boot 应用程序都会使用项目根目录中application.properties
的文件夹中的 in 而不是in文件夹。config
application.properties
src > test > resources
因此,作为运行测试时的黑客,我打乱了application.properties
文件config
夹中的名称。
我如何告诉 Spring Bootapplication.properties
在运行测试时在测试位置使用而不接触config > application.properties
?
我不想根据我是运行应用程序还是运行测试来继续重命名文件。
解决方案
您可以在 src/main/resources 文件夹下创建一个配置文件 ( application-test.properties
),然后
@TestPropertySource(locations = "classpath:application-test.properties")
在您的测试中使用
推荐阅读
- ios - Swift - 根据自定义条件将数组转换为字典
- java - 为什么 MediaPlayer 静态方法 create() 中使用的 Uri 会抛出 java.lang.IllegalArgumentException?
- sql-server - SQL Server 之外的二进制文件
- html - 如何将字体缩放到父容器的宽度
- algorithm - 找到有向非负加权图的最短路径,避免给定子集顶点的任何顶点彼此相邻?
- reactjs - React Hook Order 错误 - 无法读取未定义的属性“长度” - 你如何解决这个问题?
- javascript - 在 Javascript/react 中合并两个数组
- c - 字符串比较的更好方法
- reactjs - 问题:React-Native - 键盘在 TextInput 的每次击键时关闭
- r - 将行值组旋转到 R 中的列中