java - 通过 main 的命令行参数传递活动配置文件
问题描述
我正在尝试从我的 junit 测试用例中调用 Spring boot 主应用程序,如下所示并将配置文件设置为参数。
MyApplication.main(new String[] {"-Dspring.profiles.active=local"});
我有从 yml 文件读取的 @Value 配置。
yml
spring:
profiles: local
subdivisions: test
爪哇
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
测试用例
@RunWith(SpringJUnit4ClassRunner.class)
public class MyApplicationTest {
@Test
public void testMainApplication() {
MyApplication.main(new String[] {"-Dspring.profiles.active=local"});
}
}
当我运行我的测试用例时,我收到了这个错误 Could not resolve placeholder 'subdivisions' in value "${subdivisions}"
。
我猜测未设置导致此错误的活动配置文件。
我尝试了以下选项来传递 Spring Boot 主应用程序的活动配置文件
MyApplication.main(new String[] {"spring.profiles.active=local"});
提前致谢。对此的任何帮助表示赞赏..
解决方案
我遇到了同样的问题,发现这种语法对我有用:
@Test
void main() {
MyApplication.main(new String[]{"--spring.profiles.active=dev"});
}
推荐阅读
- python - 使用 for 循环创建字典列表
- java - 收集流时如何使用 Guava 的 Multisets.toMultiSet()?
- django - Google Cloud Platform 或 Google ML Engine 上的对象检测 Django Rest API 部署
- java - 我想将文件的内容按行拆分为 arrayLists 并在正确的标识符下重建它们
- drake - 网格_不_支持接近度:在 DRAKE
- vue.js - Vue JS - 如何将路由 (:id) 转换为 i18N 翻译?
- c++ - 在 C++17 中,非递减是否一定意味着排序,即使使用不是全序的比较器?
- http-live-streaming - HLS Fairplay DRM 内容我使用 videojs 和 videojs-contrib-eme 从 DRM 服务器获取许可证密钥时出错
- html - 用替换删除文本
- haskell - Haskell作业中类型构造函数的问题