Junit类-加载ApplicationContext失败



我正在开发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的)构建过程以类似的方式工作。

相关内容

  • 没有找到相关文章

最新更新