博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring框架下的单元测试方法
阅读量:4696 次
发布时间:2019-06-09

本文共 1288 字,大约阅读时间需要 4 分钟。

介绍在Spring的框架下,做单元测试的两种办法。

一、使用spring中对Junit框架的整合功能

除了junit4和spring的jar包,还需要spring-test.jar。引入如下依赖:

  1. <span style="font-size:18px;"><span style="white-space:pre">        </span><dependency>  
  2.             <groupId>org.springframework</groupId>  
  3.             <artifactId>spring-test</artifactId>  
  4.             <version>3.1.1.RELEASE</version>  
  5.         </dependency></span>  

然后测试类需要继承自AbstractJUnit4SpringContextTests,这样就可以在测试类中使用注解简单的注入需要的bean了。

  1. <span style="font-size:18px;"><span style="color:#ff0000;">@RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration({
    "classpath:applicationContext.xml"})</span>  
  3. public class ReadDaoImplTest extends<span style="color:#ff0000;"> AbstractJUnit4SpringContextTests</span>{  
  4.     @Resource ReadDao readDao;  
  5.       
  6.     @Test  
  7.     public void getListTest(){  
  8.         List<Client> clientList = readDao.getList("client.test", null);  
  9.           
  10.         for(Client c:clientList){  
  11.             System.out.println(c.getVersionNum());  
  12.         }  
  13.     }  
  14. }  
  15. </span>  

 

二、手动加载spring的配置文件,并启动spring容器

  1. public class ReadDaoImplTest {  
  2.       
  3.     public  static void main(String[] args){  
  4.         ClassPathXmlApplicationContext context = <span style="color:#ff0000;">new ClassPathXmlApplicationContext("applicationContext.xml");</span>  
  5.   
  6.         context.start();  
  7.   
  8.         ReadDao fqaService = (ReadDao) context.getBean("readDao");  
  9.         System.out.println(fqaService);  
  10.     }  
  11.       
  12. }  

用这种方式测试,只需要Ctrl+F11就行了

 

 

转载于:https://www.cnblogs.com/kuangyuping/p/3861861.html

你可能感兴趣的文章
读《图解HTTP》有感-(返回结果的HTTP状态码)
查看>>
操作数栈
查看>>
转:文本分类问题
查看>>
tensorflow_python中文手册
查看>>
Vs2012在Linux应用程序开发(3):加入新平台hi3516
查看>>
adb shell am 的用法
查看>>
实现自动点击
查看>>
MVP开发模式的理解
查看>>
Unity多开的方法
查看>>
File类中的list()和listFiles()方法
查看>>
我的VS CODE插件配置 主要针对.NET和前端插件配置
查看>>
关于js中的事件
查看>>
一致性哈希算法运用到分布式
查看>>
决策树和随机森林->信息熵和条件熵
查看>>
iOS10 UI教程视图和子视图的可见性
查看>>
Maven学习笔记
查看>>
FindChildControl与FindComponent
查看>>
1、简述在java网络编程中,服务端程序与客户端程序的具体开发步骤?
查看>>
C# Web版报表
查看>>
中国城市json
查看>>