Overriding multiple properties for junit test using spring or camel property placeholder in layout

I want to specify only the properties that I want to override in the test properties file with the same name in the src / test / resources folder.

A bit more ...

In the maven layout, I have a properties file that contains the expanded value to use (e.g. input.uri).

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

The properties of this file are loaded into the context.xml file using the placeholder property:

 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" /> 

I have a test properties file with the same name, app.properties , in the src / test / resources folder and override the definition of input.uri with what my junit test will use. (note that the application name does not change).

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

How could you write a junit test and / or a context.xml test file so that the properties are loaded from the src / main / resources / app.properties file , but any properties defined in the src / test / resources / app.properties file will override the files in src / main / resources / app.properties file . Without obviousness, you are loading two different files either into src / main files or into src / test junit test files - I want the property placeholder to look for the class path and select the correct values.

+6
source share
1 answer

You will have to specify a different name - if both properties in the main and test have the same name, all properties in one or another will take effect.

Instead, this approach worked for me:

In src/main/resources/META-INF/spring/context.xml do the following:

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

In the test-context.xml file:

 <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> 

This will override the properties for you, while preserving the undefined values ​​from the source file.

+4
source

Source: https://habr.com/ru/post/923415/


All Articles