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就行了

最新内容请见作者的GitHub页:http://qaseven.github.io/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值