我正在开发Spring框架。上了一堂junit课但我不能正确加载运行@Test方法所需的xml文件在junit类。在我的例子中
- xml文件放在
WEB-INF
文件夹下 - 单元测试类在
test/<package_name>
下
请告诉我如何正确声明
中的xml文件@ContextConfiguration
@ContextConfiguration( locations={ "classpath:/applicationContext.xml",
"classpath:/applicationDatabaseContext.xml" })
误差:
在允许TestExecutionListener时捕获异常(org.springframework.test.context.support.DependencyInjectionTestExecutionListener@48fa48fa)to prepare test instance [] java.lang.IllegalStateException: Failed to加载ApplicationContext
如果您正在使用Maven(推荐),那么将Spring配置文件放在标准位置src/main/resources
中(对于任何特定于测试的配置,则放在src/test/resources
中),然后在构建期间将这些文件复制到target/classes
目录中。
您可以在@ContextConfiguration
中引用这些内容,只需:
@ContextConfiguration(locations = { "/applicationContext.xml",
"/applicationContext-test.xml"})
如果您不使用Maven,我仍然建议您对源代码和工件使用标准目录布局,并使您的(可能是基于ant的)构建过程以类似的方式工作。