日期:2014-05-16  浏览次数:20764 次

dbunit和poi版本不兼容的问题
   使用unitiles+dbunit 2.4.8,由于我的测试数据使用xls文件,所以需要用到poi库,但是dbunit只支持poi 3.2版本的,高版本时会报:
引用
java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFDateUtil.isCellDateFormatted(Lorg/apache/poi/hssf/usermodel/HSSFCell;)Z
at org.dbunit.dataset.excel.XlsTable.getValue(XlsTable.java:145)
at com.book.test.unitilsext.dbunit.XslTableWrapper.getValue(XslTableWrapper.java:40)
at org.unitils.dbunit.dataset.SchemaFactory.addRows(SchemaFactory.java:128)
at org.unitils.dbunit.dataset.SchemaFactory.addTables(SchemaFactory.java:88)
at org.unitils.dbunit.dataset.SchemaFactory.createSchemaForDbUnitDataSet(SchemaFactory.java:45)
at org.unitils.dbunit.util.DataSetAssert.assertEqualDbUnitDataSets(DataSetAssert.java:70)
at org.unitils.dbunit.DbUnitModule.assertDbContentAsExpected(DbUnitModule.java:261)
at org.unitils.dbunit.DbUnitModule$DbUnitListener.afterTestMethod(DbUnitModule.java:563)
at org.unitils.core.Unitils$UnitilsTestListener.afterTestMethod(Unitils.java:301)
at org.unitils.UnitilsTestNG.run(UnitilsTestNG.java:114)


   参见:http://www.dbunit.org/dependencies.html
   由于dbunit团队好像已经好久没有发布版本(最新发布是2010年9月),但是trunk版本已经有人声称解决了这个问题:
   参见:http://dbunit.svn.sourceforge.net/viewvc/dbunit?view=revision&revision=1132
   但是,我不想麻烦自己编译dbunit,于是,我把poi换成3.2版本的,又报:

引用
java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
at org.dbunit.dataset.excel.XlsDataSet.<init>(XlsDataSet.java:75)


   原因是我对org.unitils.dbunit.datasetfactory.DataSetFactory进行了扩展,以支持多Sheet的XLS文件,这里我用到了POI高版本的类。
   好像进入了死锁状态,高低版本都不能用了!
  
   后来看到了这篇文章 :
http://mail-archives.apache.org/mod_mbox/poi-user/200910.mbox/%3Calpine.LFD.2.00.0910161316030.29288@grenache%3E说dbunit的二进制版本和>poi 3.2的不兼容,但是源码却是兼容的,需要下载dbunit的源码版本,更改pom.xml中的poi版本号,再编译,这样dbunit就可以支持高版本的poi的(即>3.2),没有办法,只依计而行。
  
步骤1:更改dbunit的pom.xml

   下载dbunit2.4.8版本,更改其pom.xml
<dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <!--原来是3.2,改为3.7 -->
      <version>3.7</version>
      <exclusions>
        <exclusion>
          <artifactId>log4j</artifactId>
          <groupId>log4j</groupId>
        </exclusion>
        <exclusion>
          <artifactId>commons-logging</artifactId>
          <groupId>commons-logging</groupId>
        </exclusion>
      </exclusions>
      <optional>true</optional>
    </dependency>

 
步骤2:重新编译

   mvn clean compile -Pmysql
   说明dbunit提供了很多数据库的方案,这时我选择mysql.

步骤3:将新编译的dbunit上传到私服nexus中
注:如果没有使用maven,直接将这个编译包放到lib目录中即可。

步骤4:更新自己项目的pom.xml,引用这个dbunit的构件


  这样着腾后,终于成功了。