将 spring-test 与 JUnit 一起使用来读取特定于测试方法的属性文件的最佳方法是什么? 下面的 Bean 说明了搜索方法级、类级、包级和根级属性文件的意图:
<bean name="properties" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" >
<property name="locations">
<list>
<value>classpath:properties.xml</value>
<value>classpath:*/properties.xml</value>
<value>classpath:*/*/properties.xml</value>
<value>classpath:*/*/*.properties.xml</value>
</list>
</property>
</bean>
我考虑过使用/子类化PropertySourcesPlaceholderConfigurer
或SpringMethodRule
,但到目前为止,我还没有想到一个可行或优雅的解决方案。 spring-test 提供了各种有前途的类级注释,但绊脚石是我希望能够找到特定于方法的文件,例如:
classpath:myPackage/myClass/myMethod.properties.xml
如果你只是在寻找一种算法来帮助你基于当前方法查找类路径资源,你可以从Spring TestContext Framework中的ServletTestExecutionListener
中汲取灵感,特别是detectDefaultScript()
方法。
问候
Sam(Spring TestContext Framework的作者)