首页 > 解决方案 > 运行测试时如何指示 Spring Boot 使用测试资源中的 application.properties 文件

问题描述

我有以下 Spring Boot 应用程序的项目结构。

在此处输入图像描述

问题是,每当我运行测试时,Spring Boot 应用程序都会使用项目根目录中application.properties的文件夹中的 in 而不是in文件夹。configapplication.propertiessrc > test > resources

因此,作为运行测试时的黑客,我打乱了application.properties文件config夹中的名称。

我如何告诉 Spring Bootapplication.properties在运行测试时在测试位置使用而不接触config > application.properties

我不想根据我是运行应用程序还是运行测试来继续重命名文件。

标签: javaspringspring-boot

解决方案


您可以在 src/main/resources 文件夹下创建一个配置文件 ( application-test.properties),然后 @TestPropertySource(locations = "classpath:application-test.properties")在您的测试中使用


推荐阅读