Junit类 - 无法加载ApplicationContext

Lal*_*yal 4 java junit spring

我正在研究Spring Framework.并制作了一个junit类,但我无法正确加载在junit类中运行@Test方法所需的xml文件.就我而言

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

请建议我正确声明xml文件的方法

@ContextConfiguration

@ContextConfiguration( locations={ "classpath:/applicationContext.xml",
        "classpath:/applicationDatabaseContext.xml" })
Run Code Online (Sandbox Code Playgroud)

错误:

允许TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@48fa48fa]准备测试实例[]时出现异常java.lang.IllegalStateException:无法加载ApplicationContext

mil*_*use 9

如果您正在使用Maven(推荐),那么将Spring配置文件放在标准位置 src/main/resources(以及src/test/resources任何特定于测试的配置),然后在构建期间,这些文件将被复制到target/classes目录中.

您可以在以下@ContextConfiguration简单地引用这些:

@ContextConfiguration(locations = { "/applicationContext.xml",
                                    "/applicationContext-test.xml"})
Run Code Online (Sandbox Code Playgroud)

如果你没有使用Maven,我仍然建议使用标准目录布局来获取源和工件,并使你的(可能是基于Ant的)构建过程以类似的方式工作.