Junit单元测试Spring 读取WEB-INF下的配置文件
来源:互联网 发布:淘宝卖家app哪个好用 编辑:程序博客网 时间:2024/05/02 00:08
转载地址:http://www.mrfeng.net/post/189.html
假设Spring配置文件为applicationContext.xml
一、Spring配置文件在类路径下面
在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下)。
以下是我的项目,因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下
这时候,在代码中可以通过
ApplicationContext applicationContext = newClassPathXmlApplicationContext("applicationContext.xml");
然后获取相应的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
二、Spring配置文件在WEB-INF下面
当然在做J2EE开发时,有些人习惯把Spring文件放在WEB-INF目录(虽然更多人习惯放在类路径下面)下面;或者有些Spring配置文件是放在类路径下面,而有些又放在
WEB-INF目录下面,如下图。
这时候,在代码中就不可以使用ClassPathXmlApplicationContext来加载配置文件了,而应使用FileSystemXmlApplicationContext。
ApplicationContext applicationContext = newFileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");
然后获取相应的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
下面是我的一个Spring管理下的Junit测试类:
package com.sohu.group.service.external;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})
public class SuFriendServiceImplOverRMITest {
@Autowired
private SuFriendService suFriendService;
@Test
public void getUserFollowerListTest(){
List<String> list = suFriendService.getUserFollowerList("liug_talk@163.com");
System.out.println("------"+list);
}
}
- Junit单元测试Spring 读取WEB-INF下的配置文件
- Junit单元测试Spring 读取WEB-INF下的配置文件
- Junit单元测试Spring读取classes或者WEB-INF目录下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring Junit 读取Resource、WEB-INF目录下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Spring Junit 读取WEB-INF下的配置文件
- Junit 单元测试 Spring 读取WEB-INF下 xml 文件
- Spring+Junit 读取WEB-INF上的配置文件
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- 读取web-inf目录下的spring配置文件
- Spring 读取WEB-INF配置文件
- java文件读取web-inf下的spring的xml配置文件
- First Missing Positive
- android sdk镜像站点
- JUnit基础及第一个单元测试实例(JUnit3.8)
- Facebook POP Demo
- Java 延迟时间执行代码
- Junit单元测试Spring 读取WEB-INF下的配置文件
- 十六进制转成字符串形式与字符串形式转hex
- IOS程式语法之block的使用掌握
- Kafka(分布式发布订阅消息系统)
- Linux目录权限
- 三大UML建模工具Visio、Rational Rose、PowerDesign的区别
- Xposed框架使用教程】骇客必备 只需400K开启未知功能 做你的手机主宰者
- 从韩信点兵和勾股弦说起—— 漫谈基础数学的古今中外
- 图像小波分析