在测试配置文件运行期间从 settings.xml 注入属性值

问题描述

我向我的服务添加一个功能,现在我可以使用来自 rabbitMq 的消息。这在 Spring Boot 中非常有效,而且开箱即用。

但是,为了不将凭据添加到源代码管理中,我一直在尝试从用户的 settings.xml 中注入密码。当我使用其认/生产配置文件在本地启动服务时,这会按预期工作,但在我运行测试套件(测试配置文件)时却不会。

以下是我的 settings.xml 示例:

<settings
        ...
        <profiles>
                <profile>
                        <id>inject-properties</id>
                        <properties>
                                <prod.rabbit.pwd>someProdPwd</prod.rabbit.pwd>
                                <test.rabbit.pwd>someTestPwd</test.rabbit.pwd>
                        </properties>
                </profile>
        </profiles>
        <activeProfiles>
                <activeProfile>inject-properties</activeProfile>
        </activeProfiles>
</settings>

我运行 mvn help:active-profiles 来验证它是否激活:

The following profiles are active:

 - inject-properties (source: external)
 - package (source: com.myservice:myservice:1.16.0-SNAPSHOT)

然后,在我的 application.properties 中,我将属性定义如下:

spring.rabbitmq.password=@prod.rabbit.pwd@

我站起来服务,我可以看到属性被 settings.xml 中的值替换,所以这部分看起来不错。

然后,在我的 application-test.properties 中,我有

spring.rabbitmq.password=@test.rabbit.pwd@

但是当我继续运行我的测试时,它不像以前那样工作了。当rabbit bean 被spring boot context builder 实例化时,它会传递文字@test.rabbit.pwd@,这会导致认证失败并导致服务无法启动/测试无法运行。

有人遇到过这种奇怪的行为吗?我发现了这个问题,这与我不久前遇到的问题几乎相同,但它并没有真正的解决方案。 Replace property placeholders in spring profile-specific properties file

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)