单元测试使用spring注解获取bean
在实际项目开发中经常会有单元测试,单元测试中经常会用类似这样的代码片段获取spring管理的bean
@Test
public void testSendEmail(){
MessageService messageService = (MessageService) BeanFactory.getInstance().getBean("messageService");
messageService.send();
}
这样既不美观,又比较繁琐,spring引进了spring-test跟junit结合使用可以方便的得到spring bean
因为在项目中适用maven管理依赖,先在pom.xml中添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
绑定spring配置文件路径
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class BaseTest extends TestCase {
protected Logger logger = LoggerFactory.getLogger(getClass()); }
在单元测试类中集成 BaseTest
public class PostServiceTest extends BaseTest {
@Resource(name = "postService")
private PostService postService;
@Test
public void testQuery2LevelPostType() {
Map<Integer,Object> map= postService.query2LevelPostType();
System.out.println("data size:" + map.size());
}
}
这样就可以在单元测试中轻松获取spring bean了,减少了繁琐的代码也增强了代码的可读性
单元测试使用spring注解获取bean的更多相关文章
- (转)Spring注解完成Bean的定义
使用Spring注解完成Bean的定义 2010-04-21 16:48:54| 分类: spring|举报|字号 订阅 下载LOFTER客户端 通过@Autowired或@Reso ...
- 在Servlet中获取Spring注解的bean
最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...
- 使用spring手动获取Bean的时候,不能强转回它自己。
这个问题好像有点长,描述一下: 就是通过类名的方式获取Bean后,得到一个Object对象,但是这个Object不能再强转回Bean了.抛出的异常时类型转换异常. java.lang.ClassCa ...
- Spring 注解配置Bean
一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...
- Spring Boot 获取Bean对象实体
一.实现 ApplicationContextAware 接口 package com.zxguan; import org.springframework.beans.BeansException; ...
- 使用spring注解——定义bean和自动注入
对于java bean的定义和依赖配置,使用xml文件真心是不方便. 今天学习如何用注解,解决bean的定义和注入. 常用注解: 1.自动注入:@Resources,@Autowired 2.Bean ...
- Junit单元测试注入spring中的bean(转载)
转载自:http://blog.csdn.net/cy104204/article/details/51076678 一般对于有bean注入的类进行方法单元测试时,会发现bean对象并没有注入进来,对 ...
- spring中获取bean的方式
获取bean的方式 1.可以通过上下文的getBean方法 2.可以通过@Autowired注入 定义controller @RestController @RequestMapping(" ...
- spring中获取Bean
在测试类中我们获取已经装配给容器的Bean的方法是通过ApplicationContext,即 ApplicationContext ac=new ClassPathXmlApplicationCon ...
随机推荐
- Hibernate 再接触 基础配置 搭建Log4j环境 Junit日志环境等
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.aut ...
- Roslyn Cookbook
Roslyn Cookbook by Manish Vasani Publisher: Packt Publishing Release Date: July 2017 ISBN: 978178728 ...
- python使用cv2显示图片像素值
给定一张灰度图,显示这张图片的像素值 def show_image_pixel(img): ''' :param img: 需要输出像素值的图像,要求是灰度图 :return: 无返回值 ''' he ...
- vue --轮播图
轮播图,可以使用mint-ui中的swipe HTML: <Swipe :auto="4000"> <SwipeItem v-for="item in ...
- CXF使用JMS作为传输协议的配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- The CHAR and VARCHAR Types
[The CHAR and VARCHAR Types] The CHAR and VARCHAR types are declared with a length that indicates th ...
- cdh 安装系列3--cdh manager 安装 cdh 6.01 版本
安装前提是cdh manager 已经可以通过admin登录,管理台安装在192.168.20.163 一.安装自动TLS Setup Auto-TLS 1.ssh 192.168.20.163 2. ...
- 解决在linux环境安装setuptools的相关错误
RuntimeError: Compression requires the (missing) zlib module 缺少zlib包 解决方案 yum install zlib yum i ...
- maven开发工具安装
Maven安装并测试步骤: 1.下载并解压meaven.zip: 2.配置环境变量“M2_HOME”指向meaven安装目录: 3.添加“%M2_HOME%\bin;”到path环境变量中: 4.测试 ...
- Django路由配置系统,视图函数
一.路由配置系统(URLconf) URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个 ...