gpt4 book ai didi

spring - 在 Maven 布局中使用 Spring 或 Camel 属性占位符覆盖 junit 测试的一些属性

转载 作者:行者123 更新时间:2023-12-02 05:51:10 25 4
gpt4 key购买 nike

我只想在 src/test/resources 文件夹中的同名测试属性文件中指定要覆盖的属性。

更详细一点...

在 Maven 布局中,我有一个属性文件,其中包含要使用的部署值(例如 input.uri)。

src/main/resources/app.properties: 
input.uri=jms:topic:in
app.name=foo_bar

此文件的属性通过属性占位符加载到 context.xml 文件中:

src/main/resources/META-INF/spring/context.xml:
<context:property-placeholder properties-ref="springProperties"/>
<util:properties id="springProperties" local-override="true" location="classpath:app.properties" />

我在 src/test/resources 文件夹中有同名的测试属性文件 app.properties,并覆盖 input.uri em> 定义为我的 junit 测试将使用的定义。 (请注意,app.name 不会更改)。

src/test/resources/app.properties: 
input.uri=seda:in

如何编写 junit 测试和/或测试 context.xml 文件,以便从 src/main/resources/app.properties 文件加载属性,但在src/test/resources/app.properties 文件会覆盖 src/main/resources/app.properties 文件中的内容吗?不明显的是,您正在 src/main 文件或 src/test junit 测试文件中加载两个不同的文件 - 我希望属性占位符搜索类路径并选择正确的值。

最佳答案

不过,您必须提供不同的名称 - 如果主属性和测试中的属性具有相同的名称,则其中一个或另一个中的整个属性将生效。

相反,这样的方法对我有用:

在您的src/main/resources/META-INF/spring/context.xml中执行以下操作:

<context:property-placeholder location="classpath:app.properties" local-override="true" properties-ref="springProperties"/>
<util:properties id="springProperties">
</util:properties>

test-context.xml 文件中:

<import resource="classpath:/META-INF/spring/context.xml">
<util:properties id="springProperties"> <!-- or refer to a overriding file -->
<prop key="input.uri">seda.in</prop>
</util:properties>

这将为您覆盖属性,同时保留原始文件中未覆盖的值。

关于spring - 在 Maven 布局中使用 Spring 或 Camel 属性占位符覆盖 junit 测试的一些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12060692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com