Junit单元测试Spring读取classes或者WEB-INF目录下的配置文件
2016-11-06 21:25
513 查看
假设Spring配置文件为spring-context.xml
一、Spring配置文件在类路径下面(maven项目)
在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下)。
因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下
这时候,在代码中可以通过
ApplicationContext applicationContext = newClassPathXmlApplicationContext("spring-context.xml");
然后获取相应的spring管理的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-context.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
二、Spring配置文件在WEB-INF下面(maven项目)
当然在做J2EE开发时,有些人习惯把Spring文件放在WEB-INF目录(虽然更多人习惯放在类路径下面)下面;或者有些Spring配置文件是放在类路径下面,而有些又放在WEB-INF目录下面。
这时候,在代码中就不可以使用ClassPathXmlApplicationContext来加载配置文件了,而应使用FileSystemXmlApplicationContext。
ApplicationContext applicationContext = newFileSystemXmlApplicationContext("src/main/webapp/WEB-INF/spring-context.xml");
然后获取相应的spring管理的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring-context.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
下面是一个抽象的Junit测试基类,其它测试类继承此类然后通过注解的方式即可获得spring管理的bean:
一、Spring配置文件在类路径下面(maven项目)
在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下)。
因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下
这时候,在代码中可以通过
ApplicationContext applicationContext = newClassPathXmlApplicationContext("spring-context.xml");
然后获取相应的spring管理的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-context.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
二、Spring配置文件在WEB-INF下面(maven项目)
当然在做J2EE开发时,有些人习惯把Spring文件放在WEB-INF目录(虽然更多人习惯放在类路径下面)下面;或者有些Spring配置文件是放在类路径下面,而有些又放在WEB-INF目录下面。
这时候,在代码中就不可以使用ClassPathXmlApplicationContext来加载配置文件了,而应使用FileSystemXmlApplicationContext。
ApplicationContext applicationContext = newFileSystemXmlApplicationContext("src/main/webapp/WEB-INF/spring-context.xml");
然后获取相应的spring管理的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring-context.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
下面是一个抽象的Junit测试基类,其它测试类继承此类然后通过注解的方式即可获得spring管理的bean:
package com.junit.test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) #@ContextConfiguration(locations = { "classpath:spring-context.xml" }) @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring-context.xml" }) public abstract class AbstractBaseTest extends AbstractTransactionalJUnit4SpringContextTests { }
相关文章推荐
- 读取web-inf目录下的spring配置文件
- Spring Junit 读取WEB-INF下的配置文件
- java文件读取web-inf下的spring的xml配置文件
- Spring 读取WEB-INF配置文件
- Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法
- Spring+Junit 读取WEB-INF上的配置文件
- Spring Junit 读取Resource、WEB-INF目录下的配置文件
- eclipse 部署项目到Tomcat spring-web 的jar包部署失败 在tomcat 中的WEB-INF/classes 目录下有我该项目工作空间的所有文件及目录
- Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法
- javaweb读取任意目录的下的properties配置文件(解决普通java类读web-inf下任意目录)
- Spring Junit 读取WEB-INF下的配置文件
- Eclipse下无法编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法
- Spring Junit 读取WEB-INF下的配置文件
- Eclipse下无法编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法
- Spring Junit 读取WEB-INF下的配置文件
- Java读取WEB-INF目录下的properties配置文件
- Spring 读取WEB-INF配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring+Junit 读取WEB-INF上的配置文件
- Junit单元测试Spring 读取WEB-INF下的配置文件