我可能错误地想到了这一点,但这就是我要反对的:
我正在将Spring Resource对象注入我的应用程序,为我提供安全证书的位置,例如:<property name="certificateResource" value="SomeCert.p12" />
其中certificateResource的类型为org.springframework.core.io.Resource
在JUnit下运行Resource是一个类路径资源,一切都很好。当在Tomcat下部署为战争时,资源处于Servlet上下文中,并且需要WEB-INF/classes/
作为证书的前缀。
我已经尝试了许多资源前缀和通配符组合,但是不能提出满足两个上下文的单个字符串。到目前为止,“解决方案”是覆盖src/test/resources/test-applicationContext.xm
l中的bean定义,但这意味着将字符串保存在两个位置。
对于更好的解决方案的任何指示都将不胜感激,谢谢。
答案 0 :(得分:2)
我尝试使用spring-test和spring-webmvc并使用xml configs中引用的资源来创建一个小但描述性的应用程序,而这些xml由生产和测试弹簧配置重用。以下是我收到的内容:github.com/isopov/spring-resource-test
中心是测试配置中的prodcution xml config的引用:
<import resource="file:src/main/webapp/WEB-INF/applicationContext.xml" />
也可以不从另一个导入一个xml,但为每个测试提供几个配置:
@ContextConfiguration(locations = { "classpath:test-applicationContext.xml",
"file:src/main/webapp/WEB-INF/applicationContext.xml" })
资源本身位于src/main/resources
(我假设您正在使用Maven或从“标准目录布局”派生的东西),因此始终可以从任何弹簧配置使用"classpath:hello.txt"
访问。
我使用maven build测试了这个,作为Web应用程序并在Eclipse中运行UT。