spring - Spring 应用程序 - 通过测试属性覆盖应用程序属性
问题描述
我是春天的新手,如果有人能帮助我解决这个问题,我将不胜感激。
我有一个 Spring Boot 应用程序 App1,它分别在主包和测试包中具有以下数据库属性:
src/main/resources/app1.properties
src/test/resources/app1-test.properties
假设 app1.properties 包含 oracle 数据库属性,而 app1-test.properties 包含 h2 数据库属性
通常,当我启动 App1 应用程序时,它会从 app1.properties 中获取属性
我的要求是,当我创建应用程序的测试实例(比如 TestApp1)时,应该选择 app1-test.properties 而不是 app1.properties。
我怎样才能做到这一点?请帮忙。
解决方案
如果您指的是在为您的应用程序运行测试类时,您可以在类上方包含注释 @ActiveProfiles("test") 以包含 app1-test.properties 配置。
类的顶部应该是这样的:
@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class YourTestClassName {
推荐阅读
- node-rules - 创建一个新的 RuleEngine 实例需要多少资源?(或者如何处理复杂的多步骤过程)
- php - FPDF - 处理特殊字符
- json - 如何从 TypeScript 中的 JSON 对象数组中删除多个项目
- javascript - 世博会文档选择器在构建后无法正常工作
- pandas - 使用 Pandas 实现 Excel 自动化
- python - 如何使用 Python 从百分比中获得同等成绩
- javascript - 根据状态在每行上动态隐藏/显示图标
- python - 导入 cv2 时 Python 立即被杀死
- package - 了解 apt install 命令的中断错误信息
- javascript - 我如何在不同的部分(例如 h2,
) 的内容部分?