zoukankan      html  css  js  c++  java
  • Spring框架下的单元测试方法

    介绍在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就行了

     

  • 相关阅读:
    心理分析:爱挑刺的人是什么心理
    推荐几个最好用的CRM软件,本人亲测
    Xamarin安装和跳坑指南
    一个简单的游戏开发框架(七.动作Motion)
    一个简单的游戏开发框架(六.行为Action)
    一个简单的游戏开发框架(五.对象Object)
    一个简单的游戏开发框架(四.舞台Stage)
    一个简单的游戏开发框架(三.事件管理)
    一个简单的游戏开发框架(二.对外接口)
    一个简单的游戏开发框架(一.框架的意义)
  • 原文地址:https://www.cnblogs.com/kuangyuping/p/3861861.html
Copyright © 2011-2022 走看看