zoukankan      html  css  js  c++  java
  • 利用spring的测试组建,测试bean

    我们的项目是用spring构建的,如果用junit编写测试类

    需要手动的初始化spring容器,然后从spring容器中得到想要测试的bean,才能够开始真正编写测试业务。

    此时可以使用spring提供的测试组建,快速的初始化spring容器并注入需要的bean到测试类实例中。

    spring提供的测试组建有许多用法,下面只列出最简单常用的一个测试类“AbstractDependencyInjectionSpringContextTests”的写法

    import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    import java.util.HashMap;
    import java.util.Map;
    /**
    * Created by IntelliJ IDEA.
    * User: Luo
    * Date: 2010-9-21
    * Time: 10:22:38
    */
    public class UniversalCardBatchServiceTest extends AbstractDependencyInjectionSpringContextTests {

    //这个是我们测试业务需要的业务bean,这里我们使用set方法来注入,更方便的可以用注解来自动注入,例如@Autowire
    UniversalCardBatchService universalCardBatchService;
    //这里写我们的测试业务
    public void testInsert(){
    Map map
    = new HashMap();
    universalCardBatchService.insertUniversalCardBatch(map);
    }
    //重写这个方法,用来指定spring的配置文件,可以写多个文件
    //我们的项目中spring.xml中引入其他所有的配置文件,所以只需要指定一个入口文件spring.xml就可以了
         //classpath:表示从classes目录下读取

    @Override
    protected String[] getConfigLocations() {
    String[] location
    = {"classpath:spring.xml"};
    return location;
    }
    //我们用set注入,当然set方法是不能少的
    public void setUniversalCardBatchService(UniversalCardBatchService universalCardBatchService) {
    this.universalCardBatchService = universalCardBatchService;
    }
    }

  • 相关阅读:
    make -j 8参数的作用
    使用请求头认证来测试需要授权的 API 接口
    查看Linux系统的平均负载
    服务器负载均衡的基本功能和实现原理
    Oracle RAC学习笔记:基本概念及入门
    详解物化视图(汇总比较有用的资料)
    程序优化注意的一些点
    PR 审批界面增加显示项方法
    Most Common Solutions to FRM-41839 and .tmp Files Not Being Deleted
    APPCORE Routine APIs
  • 原文地址:https://www.cnblogs.com/baibaluo/p/2071233.html
Copyright © 2011-2022 走看看