Junit单元测试 加载两种路径的Spring配置文件applicatonContext.xml

        前言:今天工作中遇到一个问题,J2EE项目使用Spring,Spring配置文件applicationContext.xml放在和类路径平级的一个conf目录下,在使用Junit进行单元测试的时候,无法通过注解@ContextConfiguration加载applicationContext.xml配置文件,这里整理出配置文件的两种路径的加载:

一、类路径下的配置文件加载(src目录下):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})

二、WEB-INF路径下的配置文件加载:

这里的WEB-INF下加载配置文件也就是对应了我上面遇到的问题(applicationContext.xml放在和类路径平级的一个conf目录下),两者是一样的。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:WebRoot/WEB-INF/config/applicationContext.xml"})

猜你喜欢

转载自blog.csdn.net/PORSCHE_GT3RS/article/details/82228349
今日推荐