Unitils在集成DBunit时,如果数据库是mysql时,就会出现一些如下:
org.unitils.core.UnitilsException: Error inserting test data from DbUnit dataset for method......
org.dbunit.dataset.NoSuchColumnException......
出现如上两个报错的原因是:unitils默认使用的是“DefaultMetadataHandler.java”这个类去加载数据库信息,从而得不到数据库schema的值,DefaultMetadataHandler.java中的实现代码如下:
boolean areEqual = areEqualIgnoreNull(catalog, catalogName, caseSensitive) && areEqualIgnoreNull(schema, schemaName, caseSensitive) && areEqualIgnoreNull(table, tableName, caseSensitive) && areEqualIgnoreNull(column, columnName, caseSensitive);
这个时候的schemaName是空的,但是传进来的schema是有值的,从而报错!
解决方案两种:
1、改源代码,去掉catalog和schema的比较
2、写一个拓展类,去继承DbUnitModule,代码如下:
import org.dbunit.database.DatabaseConfig; import org.dbunit.ext.mysql.MySqlDataTypeFactory; import org.dbunit.ext.mysql.MySqlMetadataHandler; import org.unitils.dbunit.DbUnitModule; import org.unitils.dbunit.util.DbUnitDatabaseConnection; public 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; } }
然后修改unitils-core的jar包下的unitils-default.properties文件,
unitils.module.dbunit.className=org.unitils.dbunit.DbUnitModule 更改为如下:
unitils.module.dbunit.className=org.unitils.dbunit.MySqlDbUnitModule
如果你使用自己定义的配置文件unitils.properties,则只需要修改自定义配置文件即可,就可以不需要修改jar中的unitils-default.properties
当然出现这些问题的前提是所用数据库是mysql的情况下,以及解决方案~
评论列表
at org.unitils.core.dbsupport.DbSupportFactory.getDbSupport(DbSupportFactory.java:77)
at org.unitils.core.dbsupport.DbSupportFactory.getDefaultDbSupport(DbSupportFactory.java:56)
at org.unitils.dbunit.DbUnitModule.getDefaultDbSupport(DbUnitModule.java:755)
at org.unitils.dbunit.DbUnitModule.getDataSetFactory(DbUnitModule.java:725)
at org.unitils.dbunit.DbUnitModule.getDataSetFactory(DbUnitModule.java:713)
at org.unitils.dbunit.DbUnitModule.getDataSet(DbUnitModule.java:441)
at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:261)
at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:187)
... 24 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.unitils.core.dbsupport.DbSupport.determineIdentifierQuoteString(DbSupport.java:506)
... 33 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at org.unitils.core.dbsupport.DbSupportFactory.getDefaultDbSupport(DbSupportFactory.java:56)
at org.unitils.dbunit.DbUnitModule.getDefaultDbSupport(DbUnitModule.java:755)
at org.unitils.dbunit.DbUnitModule.getDataSetFactory(DbUnitModule.java:725)
at org.unitils.dbunit.DbUnitModule.getDataSetFactory(DbUnitModule.java:713)
at org.unitils.dbunit.DbUnitModule.getDataSet(DbUnitModule.java:441)
at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:261)
at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:187)
... 24 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.unitils.core.dbsupport.DbSupport.determineIdentifierQuoteString(DbSupport.java:506)
... 33 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
本文转自一米一阳光博客园博客,原文链接:http://www.cnblogs.com/candle806/p/3807590.html
,如需转载请自行联系原作者
我用该方法报错:
org.unitils.core.UnitilsException: Error inserting test data from DbUnit dataset for method public xxxxxxx()
at org.unitils.dbunit.DbUnitModule.insertDataSet(DbUnitModule.java:190)
at org.unitils.dbunit.DbUnitModule$DbUnitListener.beforeTestSetUp(DbUnitModule.java:791)
at org.unitils.core.Unitils$UnitilsTestListener.beforeTestSetUp(Unitils.java:273)
at org.unitils.UnitilsJUnit4TestClassRunner$TestListenerInvokingMethodRoadie.runBeforesThenTestThenAfters(UnitilsJUnit4TestClassRunner.java:181)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.unitils.UnitilsJUnit4TestClassRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:95)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
at org.unitils.UnitilsJUnit4TestClassRunner.access$000(UnitilsJUnit4TestClassRunner.java:42)
at org.unitils.UnitilsJUnit4TestClassRunner$1.run(UnitilsJUnit4TestClassRunner.java:60)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:67)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
Caused by: org.unitils.core.UnitilsException: Unable to determine identifier quote string.
at org.unitils.core.dbsupport.DbSupport.determineIdentifierQuoteString(DbSupport.java:516)
at org.unitils.core.dbsupport.DbSupport.init(DbSupport.java:94)