Spring JUnit测试加载位于src/test/resources之外的文件的属性

huangapple go评论68阅读模式
英文:

Spring JUnit Test load properties of file outside of src/test/resources

问题

我想将一个属性文件加载到Spring中,但是Spring无法在这个XML中找到该属性文件。

该文件位于 /MyProject/MyModule/testData/testProperties.properties

测试位于 /MyProject/MyModule/src/test/MyTest.java

如何让Spring从正确的路径加载?

<bean id="properties" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>testData/testProperties.properties</value>
        </list>
    </property>
</bean>
英文:

I want to load a properties file into Spring,but Spring cannot find the properties file with this XML.

The File is at /MyProject/MyModule/testData/testProperties.properties

The Test is at /MyProject/MyModule/src/test/MyTest.java

How to make Spring load from the correct path?

&lt;bean id=&quot;properties&quot; class=&quot;org.springframework.context.support.PropertySourcesPlaceholderConfigurer&quot;&gt;
	&lt;property name=&quot;locations&quot;&gt;
		&lt;list&gt;
			&lt;value&gt;testData/testProperties.properties&lt;/value&gt;
		&lt;/list&gt;
	&lt;/property&gt;
&lt;/bean&gt;

答案1

得分: 1

你可以尝试使用 file: 前缀

&lt;value&gt;file:/MyProject/MyModule/testData/testProperties.properties&lt;/value&gt;
英文:

You can try using file: prefix

&lt;value&gt;file:/MyProject/MyModule/testData/testProperties.properties&lt;/value&gt;

huangapple
  • 本文由 发表于 2020年9月10日 04:09:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63818880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定