基于spring的unittest

本文提供了一个Spring单元测试的抽象基类示例,通过继承该类可以简化各业务服务层单元测试的编写工作。示例中包含了如何初始化及销毁Spring上下文中的Bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有网友反应,看了http://blog.youkuaiyun.com/hongbo781202/archive/2005/04/27/365382.aspx后,还是不知道怎么做spring的unit test,下面把 自己写的抽象类给出来,应该容易懂了。

public abstract class AbstractTest extends AbstractTransactionalDataSourceSpringContextTests{
 public String[] getConfigLocations()
 {
  String[] config= new String[]{"frame-applicationContext.xml"};
  
  return config;
 }
 protected RoleInfoService roleInfoService;
 protected OperationInfoService operationInfoService;
 protected UserBaseInfoService userService;
 protected void onSetUpInTransaction() throws Exception {
        super.onSetUpInTransaction();
        //this.setPopulateProtectedVariables(true);
        roleInfoService = (RoleInfoService) this.applicationContext.getBean("roleInfoService");
        operationInfoService=(OperationInfoService)this.applicationContext.getBean("operationInfoService");
        userService = (UserBaseInfoService) this.applicationContext.getBean("userBaseInfoService");

    }

    protected void onTearDownInTransaction() {
     roleInfoService = null;
     operationInfoService=null;
     userService = null;
    }
}

每个service的独立的unit test,只要继承该类就可以了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值