java - 使用junit在spring测试中加载属性文件
问题描述
我正在尝试为我们的 Spring 应用程序创建一些 JUnit 测试。为了加载上下文,我需要访问以下目录树中的 2 个属性文件:
-settings
-default (properties common to every developer and enviroment)
-common.properties
-dev (properties regarding my local environment, ignored in commits)
-environment.properties
-src (settings is in the same level as src folder)
-...
我们还有用于生产的文件夹,qa... 与 dev 和 default 处于同一级别。
我正在尝试使用@PropertiesSources 加载这两个文件,但对于我所读到的内容,我应该执行以下操作:
@PropertiesSource("classpath:../settings/default/common.properties")
但这行不通。以前有人遇到过这个问题吗?
奖金问题:
我的个人文件夹“dev”对于这里的每个人来说都不一样。有些人称它为“本地”或“本地主机”......我们可以同意相同的名称,但是当我们打包应用程序时,我们传递了一个像 -Dsettings=dev 这样的 maven 参数,并且我们有一个只包含该文件的插件在战争中。
如果我能在测试中做类似的事情,那就太好了,但如果不能,我们只会为每个人选择相同的名字。
提前致谢,
贡萨洛
解决方案
我找到了正确的配置:
@PropertiesSource("file:settings/default/common.properties")
推荐阅读
- hyperledger-composer - 如何将 hyperledger composer 与 ipfs 结合起来?
- cpu-architecture - 冲突和强制缓存未命中有什么区别?
- sockets - Erlang: Connecting to a server, and receiving input back on the same socket
- android - 哪个更有效 - 先设置滤色器然后设置位图或设置位图然后设置滤色器?
- react-native - 获取 FlatList 中单击项目的索引 react Native
- jquery - 值更改时用于验证 2 个字段的 Jquery
- matlab - 如何从图像矩阵中检测边缘
- java - Spring ExceptionHandler for Request method 'POST' not supported
- bash - 删除常规文本文件中的字母间距
- mysql - MYSQL; 根据表 A 和表 B 的连接更新表 A