JUnit:如何按Spring的意图访问Spring配置?

sjn*_*ngm 4 junit spring

有一个教程视频介绍了Spring MVC 3.0.在演示项目中,他们使用以下目录结构:

<proj>
   src
      main
         webapp
            WEB-INF
               spring
                  appServlet
                     controllers.xml
                     servlet-context.xml
                  root-context.xml
Run Code Online (Sandbox Code Playgroud)

假设我有一个Maven支持的项目,我想用Spring的配置编写JUnit测试.目前我们使用JUnit 4.8.2.这显然需要加载上面列出的三个文件.

在测试中我可以使用注释

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:/WEB-INF/spring/**/*.xml")
Run Code Online (Sandbox Code Playgroud)

但是,它找不到XML文件.我看了一下classpath并注意到,默认情况下只包括<proj>/target/classes<proj>/target/test-classes.

一个显而易见的解决方案是为类路径添加正确的路径,但我不知道这是不是春天的人想到的.

因此,我的问题是:我需要做什么来加载配置文件,同时让它看起来好像我是使用Spring的全程编码器?

小智 5

另一种选择是使用文件系统资源加载器:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
Run Code Online (Sandbox Code Playgroud)