怎样应聘业务员:再讨论spring的单元测试
来源:百度文库 编辑:中财网 时间:2024/04/20 16:31:32
在我以前一篇文章关于spring的文章中仔细讨论过spring单元测试策略,根据最近一些经验。今天再写一点自己的经验:
1.需要把单元测试时不需要的东西拿掉,比如缓存,安全等,这就需要怎么仔细考虑spring的applicationContext.xml功能。
2.DAO与Service的测试需要使用不同的策略,一般来讲,DAO单元测试只测试一个DAO类,而service需要多个DAO来支撑,有时还需要别的service支持。因此需要为DAO与service分别定义两个基类。DAO一般不需要使用事务,因此DAO测试基类继承AbstractDependencyInjectionSpringContextTests;而service需要,Service测试基类继承AbstractTransactionalDataSourceSpringContextTests
3.那怎么简化开发人员编写单元测试类呢?若每次都需要写很多,那他们自然不愿意写,所以架构师应该简化单元测试的编程模型上下功夫。一个原则:每个人只添加自己想要的,必要时可以覆盖全部的配置文件,完全自己来写,下面是我写的两个测试基类的demo
DAO测试基类
- public abstract class AbstractDAOTestCase extends
- AbstractDependencyInjectionSpringContextTests {
- public AbstractDAOTestCase() {
- this.setAutowireMode(AUTOWIRE_BY_NAME);
- }
- @Override
- protected String[] getConfigLocations() {
- String[] baseConfigs = new String[]{ "/spring/applicationContext.xml" };
- String[] extentConfigs = getExtentConfigs();
- int length = baseConfigs.length + extentConfigs.length;
- String[] allConfigs = new String[length];
- for (int i = 0; i < baseConfigs.length; i++) {
- allConfigs[i] = baseConfigs[i];
- }
- for (int i = 0; i < extentConfigs.length; i++) {
- allConfigs[i + baseConfigs.length] = extentConfigs[i];
- }
- return allConfigs;
- }
- //开发人员可以覆盖这个方法,添加自己的配置文件
- protected String[] getExtentConfigs() {
- return new String[] {};
- }
- }
Service测试基类
- public abstract class AbstractServiceTestCase extends
- AbstractTransactionalDataSourceSpringContextTests {
- protected UserDAO userDAO;
- public AbstractServiceTestCase() {
- this.setAutowireMode(AUTOWIRE_BY_NAME);
- }
- @Override
- protected String[] getConfigLocations() {
- String[] baseConfigs = new String[] { "spring/applicationContext.xml",
- "spring/applicationContext-transaction.xml",
- "org/codehaus/xfire/spring/xfire.xml",
- "spring/applicationContext-xfire.xml",
- "spring/applicationContext-osworkflow.xml",
- "spring/platform/*.xml" };
- String[] extentConfigs = getExtentConfigs();
- int length = baseConfigs.length + extentConfigs.length;
- String[] allConfigs = new String[length];
- for (int i = 0; i < baseConfigs.length; i++) {
- allConfigs[i] = baseConfigs[i];
- }
- for (int i = 0; i < extentConfigs.length; i++) {
- allConfigs[i + baseConfigs.length] = extentConfigs[i];
- }
- return allConfigs;
- }
- //开发人员可以覆盖这个方法
- protected String[] getExtentConfigs() {
- return new String[] {};
- }
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- }
谁有高一各科的单元测试?
久石让spring的钢琴谱
新概念2的单元测试问题
Thomas Nashe 的诗歌spring,the sweet spring 诗歌赏析
有关Spring的英语短文
spring outlook 的中文含意
关于 spring 的 小文章
数学单元测试卷六年级下册的答案
spring的源代码哪里有下载的
spring的源代码哪里有下载的?
spring 框架的介绍和好处?
jsp 与 struts hibernate spring 的关系
spring的三种意思是什么?
使用spring框架的好处和不足?
含有spring leaf的英语格言
谁有struts+hibernate+spring的例子?
哪里有Spring+Hibernate的教程
如何学习开源的Spring
需要spring整合hibernate的源代码
谁给介绍本Spring的好书
新概念2单元测试的题~有些不会的~
junit单元测试的好处有那些,如何使用?
谁有北大人教版初一六年级的单元测试题
六年级语文训练册的题目,急用^0^(单元测试四)