首页 > 解决方案 > 使用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 参数,并且我们有一个只包含该文件的插件在战争中。

如果我能在测试中做类似的事情,那就太好了,但如果不能,我们只会为每个人选择相同的名字。

提前致谢,

贡萨洛

标签: javaspringintegration-testing

解决方案


我找到了正确的配置:

@PropertiesSource("file:settings/default/common.properties")

推荐阅读