单元测试使用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 ...
随机推荐
- C# WInForm中 窗体的this.width和this.height的属性值不能大于显示器的最大分辨率
最近在做一个小项目的时候,发现在 1680x1050 分辨率显示器上写的代码,将窗体的宽度和高度 设置成了 1600×900,在高于1600×900的分辨率上缩放显示很正常, 而后转移到 分辨率低于 ...
- 吴裕雄 23-MySQL ALTER命令
当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令.开始本章教程前让我们先创建一张表,表名为:testalter_tbl. create table testalter ...
- 导出excel时设置单元格格式(避免类似0100的数字丢失前面的0)
<td style="vnd.ms-excel.numberformat:@;"><s:property value="accountCode" ...
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.
https://blog.csdn.net/watermusicyes/article/details/44963773 Context中有一个startActivity方法,Activity继承自C ...
- Using Fetch
[Using Fetch] This kind of functionality was previously achieved using XMLHttpRequest. Fetch provide ...
- SpringMVC包括哪些组件
1 映射器 1.1作用:Handlermapping根据url查找Handler 2 适配器 2.1作用:HandlerAdapter执行Handler 3 解析器 3.1作用:View ...
- CSS垂直翻转/水平翻转提高web页面资源重用性
/*水平翻转*/ .flipx { -moz-transform:scaleX(-1); -webkit-transform:scaleX(-1); ...
- VS2013 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同
1,VS2013 错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array ...
- python处理数据问题详解
连接数据库 这里需要调用pymysql包,(pip install PyMySQL) 注意pip安装时名字和程序里import的名字不一样 import pymysql # 打开数据库连接 db = ...
- 153. Find Minimum in Rotated Sorted Array (Array; Divide-and-Conquer)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...