spring单元测试

spring 提供的单元测试是强大的,spring 的单元测试很简单,封装的很好。我们要用spring 的单元测试我们写的 adddelete 等方法时候需要spring 提供的一个额外包spring-mock.jar ,我已经传上来了。你只要熟悉单元测试,编写一个测试案例,然后把继承改为 org.springframework.test.AbstractTransactionalDataSourceSpringContextTests 就可以了,此时编译器会提示你要实现。

1.      public  String[] getConfigLocations(){   

2.     

3.     String[] config =  new  String[]{ "applicationContext.xml" ,   

4.     

5.      "applicationContext-dao.xml" ,   

6.     

7.      "applicationContext-hibernate.xml" ,   

8.     

9.      "applicationContext-service.xml" };   

10.    

11.     return  config;   

12.    

13.    }   

看了大家应该明白,就是把你配置好的xml 赋值给它,然后大家就可以通过下面方法:下面的applicationContext 这个变量是你只要继承了刚才那个抽象类就可以得到的一个恒量。

FriendService friendService = (FriendService)applicationContext.getBean("friendService");

得到你的实例来进行业务逻辑测试了,是不是很简单,大家试试吧,它在此时完成以后会把数据库回滚一次,不会影响你的数据库记录,非常好。

  package com.feikecom.oa.test;

 

import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

 

import com.feikecom.message.dao.CommonPhrasesDAOImpl;

import com.feikecom.oa.vo.CommonPhrases;

import com.opensymphony.xwork2.interceptor.annotations.After;

import com.opensymphony.xwork2.interceptor.annotations.Before;

 

public class TestExample2 extends AbstractTransactionalDataSourceSpringContextTests  {

@Before  

public static void setUpBeforeClass() throws Exception {   

}   

  

@After  

public static void tearDownAfterClass() throws Exception {   

}   

@Override  

protected String[] getConfigLocations() {   

  

          String[] config = new String[] {"applicationContext_message.xml","applicationContext.xml"};   

          return config;   

}   

  

public void testDao() throws Exception {   

  

CommonPhrasesDAOImpl commonPhrasesDAO = (CommonPhrasesDAOImpl)this.applicationContext.getBean("commonPhrasesDAO");   

  

CommonPhrases common = new CommonPhrases();   

commonPhrasesDAO.update(common);   

  

}   

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值