【问题标题】:Junit class - Failed to load ApplicationContextJunit 类 - 无法加载 ApplicationContext
【发布时间】:2011-12-26 18:25:28
【问题描述】:

我正在开发 Spring 框架。并做了一个junit类 但我无法正确加载在 junit 类中运行 @Test 方法所需的 xml 文件。就我而言

  • xml 文件放在文件夹WEB-INF
  • junit 测试类在test/<package_name>

请建议我在

中声明xml文件的正确方法

@ContextConfiguration

@ContextConfiguration( locations={ "classpath:/applicationContext.xml",
        "classpath:/applicationDatabaseContext.xml" })

错误

在允许 TestExecutionListener 时捕获异常 [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@48fa48fa] 准备测试实例 [] java.lang.IllegalStateException: Failed to 加载ApplicationContext

【问题讨论】:

  • 您是否使用 maven 进行构建和测试?
  • @user1041110:请添加完整的异常跟踪。在大多数情况下,该异常的原因都写在那里,但您已将其截断。

标签: java spring junit


【解决方案1】:

如果你使用Maven(推荐),那么将你的Spring配置文件放在standard locationsrc/main/resources(和src/test/resources对于任何测试特定的配置),然后在构建期间这些文件将被复制到target/classes 目录。

您可以在@ContextConfiguration 中引用这些内容,只需:

@ContextConfiguration(locations = { "/applicationContext.xml",
                                    "/applicationContext-test.xml"})

如果您不使用 Maven,我仍然建议您使用 Standard Directory Layout 获取源代码和工件,并让您的(可能是基于 Ant 的)构建过程以类似的方式工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-27
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多