qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Spring框架下的单元测试方法

 介绍在Spring的框架下,做单元测试的两种办法。
  一、使用spring中对Junit框架的整合功能
  除了junit4和spring的jar包,还需要spring-test.jar。引入如下依赖:
<span style="font-size:18px;"><span style="white-space:pre">        </span><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
</dependency></span>
  然后测试类需要继承自AbstractJUnit4SpringContextTests,这样就可以在测试类中使用注解简单的注入需要的bean了。
<span style="font-size:18px;"><span style="color:#ff0000;">@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})</span>
public class ReadDaoImplTest extends<span style="color:#ff0000;"> AbstractJUnit4SpringContextTests</span>{
@Resource ReadDao readDao;
@Test
public void getListTest(){
List<Client> clientList = readDao.getList("client.test", null);
for(Client c:clientList){
System.out.println(c.getVersionNum());
}
}
}
</span>
  二、手动加载spring的配置文件,并启动spring容器
public class ReadDaoImplTest {
public  static void main(String[] args){
ClassPathXmlApplicationContext context = <span style="color:#ff0000;">new ClassPathXmlApplicationContext("applicationContext.xml");</span>
context.start();
ReadDao fqaService = (ReadDao) context.getBean("readDao");
System.out.println(fqaService);
}
}
  用这种方式测试,只需要Ctrl+F11就行了

posted on 2014-08-29 09:43 顺其自然EVO 阅读(258) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜