zoukankan      html  css  js  c++  java
  • 用Unitils测试BaseDao遇到的问题总结

    《Spring 3.0就这么简单》.(陈雄华,林开雄)第8章,对如何用Unitils进行测试简单介绍,下面是我用Unitils进行单元测试过程中遇到的问题的总结。

    1、设置好pom.xml依赖后,pom文件报错:Missing artifact javax.transaction:jta:jar:1.0.1B

    原因是本地maven库中缺少jta-1_0_1B-classes这个jar,需要把这个jar安装到本地库中去。
    下载包含此jar的zip包,地址:http://download.csdn.net/detail/spring123tt/6847843
    cmd到zip包的目录,运行下面的字符串
    mvn install:install-file -Dfile=./jta-1_0_1B-classes.zip -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar

    2、用Excel文件作为验证数据的输入源,执行TestNG的时候报错:
    java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
    Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook

    原因:java在操作Excel等Office文件时,需要引入poi支持,所以需要在pom.xml中加入包依赖
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
    </dependency>

    3、执行TestNG报错:
    java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFDateUtil.isCellDateFormatted(Lorg/apache/poi/hssf/usermodel/HSSFCell;)

    原因:dbunit对poi支持不够好,详见http://stamen.iteye.com/blog/1478022

    更新dbunit版本到最新的2.5.1解决

    4、执行TestNG报错:
    org.apache.commons.beanutils.ConversionException: No value specified for 'Date'

    原因:beanutils用到了时间等非内置对象时,如果对象为NULL则,会出现此异常

    我的这个测试用例的目的是将Excel中的多行数据插入到数据库里面去,很显然,要插入的数据里面Date类型出现了null值。

    先在XlsDataSetBeanFactory.createbeans方法中把读取到的数据逐行打印出来,看看到底哪些数据在捣鬼。我的Excel数据是这样的:

    而实际打印的数据却出现了:

    stu_netname:null
    stu_password:null
    stu_realname:null
    stu_registdate:null

    虽然Excel文件中有几行用看上去都是空白,但是实际上很可能是添加了空格在里面,从而引起类似的错误,解决方法就是把Excel文件中真实数据下面的几行删掉。

    另外,通过google搜索到了另外的解决方法,思路是遇到Null值得时候进行规避而不是报错:http://www.blogjava.net/javagrass/archive/2011/10/10/352856.html

    5、执行TestNG报错:
    org.dbunit.DatabaseUnitRuntimeException: At least one column is required to build a valid select statement

    起因是新版本的dbunit细化了各种数据库的MetadataHandler的处理,为每一种数据库提供了一个MetadataHandler,如MySqlMetadataHandler,Db2MetadataHandler等。而unitils并没有升级,仍然使用dbunit提供的DefaultMetadataHandler。这个DefaultMetadataHandler并不能很好的处理各个数据库之间的不同,所以会导致兼容问题。

    解决方案:写一个拓展类MySqlDbUnitModule继承 DbUnitModule,

    public final class MySqlDbUnitModule extends DbUnitModule {
     
        @Override
        public DbUnitDatabaseConnection getDbUnitDatabaseConnection(final String schemaName) {
            DbUnitDatabaseConnection result = dbUnitDatabaseConnections.get(schemaName);
            if (null != result) {
                return result;
            }
            result = super.getDbUnitDatabaseConnection(schemaName);
            result.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
            result.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler());
            return result;
        }
    }

    使用新建的MySqlDbUnitModule替换默认的DbUnitModule。这个就比较简单了,在unitils.properties中加入:
    unitils.module.dbunit.className=com.miraclesea.test.database.module.MySqlDbUnitModule

    该问题参考:http://my.oschina.net/u/719192/blog/173644

    6、我的bean声明采用,注解+自动扫描的方式:

    <context:component-scan base-package="com.huawei.happycar.dao.impl,com.huawei.happycar.service.impl" />

    我的业务里面定义为:

    @Service("studentServiceBean")
    public class StudentServiceImpl implements StudentService {}

    对studentServieBean,TestNG执行的时候就会报错(意思是找不到这个bean):
    Unable to assign the Spring bean value to field annotated with @SpringBean 
    Unable to assign the value to field: studentServiceImpl. Ensure that this field is of the correct type. Value: com.huawei.happycar.service.impl.StudentServiceImp

    如果我定义为:

    @Service("studentServiceBean")
    public class StudentServiceImpl  {}

    对studentServieBean,TestNG执行的时候没有一点问题。

    另外,如果我用Junit的方法测试,两种方法都没有问题,该问题我还没有解决,怀疑是Unitils的bug


    使用Excel作为数据源进行Unitils测试时,要注意的问题

    1、unitils配置文件的设置中如下两项要按照实际的路径进行修改

    DbUnitModule.DataSet.factory.default=com.huawei.happycartest.dataset.excel.MultiSchemaXlsDataSetFactory
    DbUnitModule.ExpectedDataSet.factory.default=com.huawei.happycartest.dataset.excel.MultiSchemaXlsDataSetFactory

    2、测试方法

    public class StudentDaoTest extends BaseDaoTest {
        @SpringBean("StudentDaoImpl")
        private StudentDaoImpl StudentDaoImpl;
        @Test
        @ExpectedDataSet("StudentDao.SaveStudents.xls")
        public void saveUsers()throws Exception  {
            List<Student> users  = XlsDataSetBeanFactory.createBeans(StudentDaoTest.class,"StudentDao.SaveStudents.xls", "t_student", Student.class);
            for(Student u:users){
                StudentDaoImpl.save(u);
            }
        }
    }

    上面的写法中StdentDao.SaveStudents.xml一定要和StudentDaoTest.class在一个目录下。


    使用Junit方法的测试

        @Test
        public void test() {
            ApplicationContext ac = new ClassPathXmlApplicationContext(
                    new String[] { "applicationContext.xml", "spring-hibernate.xml" });
            // 从Spring的IOC容器中获取bean对象
            StudentService userService = (StudentService) ac.getBean("studentServiceBean");
            // 执行测试方法
            List<Student> list = userService.listAllStudent();
            for(Student stu : list)
            {
                System.out.println(stu.getStuNetname());
            }
        }

    这个Unitils测试框架官网上维护更新非常慢,Unitils的官方最新版本是3.3,对应的发布时间是2011年12月22号。
    所以显然它不能支持Hibernate4了,另外就是Spring4估计也不是完美。
    支持决定放弃使用这个测试框架,学习一下spring test。

  • 相关阅读:
    分布式架构高可用架构篇_activemq高可用集群(zookeeper+leveldb)安装、配置、高可用测试
    @interface [SpringMVC+redis]自定义aop注解实现控制器访问次数限制
    ActiveMQ安装与持久化消息
    activemq 5.13.2 jdbc 数据库持久化 异常 找不到驱动程序
    java通过Comparable接口实现字符串比较大小排序的简单实例
    微信小程序--火车票查询
    【调试】如何使用javascript的debugger命令进行调试(重要)
    【调试】js调试console.log使用总结图解(重要)
    ajax提交表单
    一个项目的404错误处理页面
  • 原文地址:https://www.cnblogs.com/mingziday/p/4649924.html
Copyright © 2011-2022 走看看