简体   繁体   中英

DBUnit: dataset with MySQL YEAR type column

folks!

I'm having a problem using DBUnit:

In my test class, when I call DatabaseOperation.INSERT.execute(connection, dataSet) , using a FlatXmlDataSet referencing a table that contains a column of type YEAR(4) - MySQL - I get the following:

(...)
Caused by: org.dbunit.dataset.datatype.TypeCastException: Error casting value for table 'Vehicle' and column 'LaunchYear'
    at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:210)
    at net.carroramafleet.ws.utils.DbUnitHelper.execute(DbUnitHelper.java:57)
    ... 33 more
Caused by: org.dbunit.dataset.datatype.TypeCastException: Unable to typecast value <2010> of type <java.lang.String> to DATE
    at org.dbunit.dataset.datatype.DateDataType.typeCast(DateDataType.java:110)
    at org.dbunit.dataset.datatype.DateDataType.setSqlValue(DateDataType.java:141)
    at org.dbunit.database.statement.SimplePreparedStatement.addValue(SimplePreparedStatement.java:73)
    at org.dbunit.database.statement.AutomaticPreparedBatchStatement.addValue(AutomaticPreparedBatchStatement.java:63)
    at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:200)
    ... 34 more

Here's my dataset:

<dataset>
    <Vehicle 
            ID="999" 
            LaunchYear="2010" />
</dataset>

As I have mentioned above, I have a YEAR(4) type column, LaunchYear , in the table Vehicle . And DBUnit can't insert this row because of this information can't be converted correctly.

I've already tried to replace this information using DBUnit's ReplacementDataSet , but I still have problem with TypeCastException. I really can't set a valid YEAR-formatted information.

Could somebody help me?

Thanks,

Jeff

This question is a bit old but I thought I would reply as I just ran into this same issue today.

I believe this is a bug in DbUnit. BTW, I'm using 2.4.9 but did check the release notes for later releases to see if this is mentioned as a bug fix.

The YEAR column is being converted into a java.sql.Date object. The initial bug is that there is no conversion from a simple string "2016" to a java.sql.Date. That leads to the TypeCastException. Changing this field to something like "2016-08-10" gets you past this initial error but leads to a SQLException when MySql attempts to truncate the Date into a integer or short.

The only way I have been able to work around this is to add specific code in the @SetUp or @Before methods to populate the table with initial data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM