简体   繁体   中英

Unitils/DBunit and database test

I want to try to make unit test with DBUnit but I have a problem with my dataset.

Here is my persistence object:

@Entity
@Table(name = "personnes")
public class Personne implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer pk;

    @Column
    private String name;
}

And my dataset:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
    <personnes name="toto"  pk="1" />
</dataset>

My problem is with the name column, I get this error:

org.dbunit.dataset.NoSuchColumnException: personnes.NAME -  (Non-uppercase input column: name) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.

I don't understand why dbunit search a column "NAME" whereas my column is "name".

Thanks for your help.

I've been fighting this for a while, and keep coming back to this issue, which doesn't seem to have a solution yet.

In Unitils 3.4.1, they added a new property, org.dbunit.database.IMetadataHandler.implClassName

In my unitils.properties file, I added the following line

org.dbunit.database.IMetadataHandler.implClassName=org.dbunit.ext.mysql.MySqlMetadataHandler

Yes, I know, according to Unitils' website, there is no version 3.4.1, but you can get the latest version via Maven.

link to issue report

Try to set datatype factory for your databases.

All available factories can be found on this link. http://dbunit.sourceforge.net/apidocs/org/dbunit/dataset/datatype/IDataTypeFactory.html

Choose factory which belongs to your database.

Implement method setUpDatabaseConfig in your test class and set factory.

protected void setUpDatabaseConfig(DatabaseConfig config) {
   config.setProperty( DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new OracleDataTypeFactory() );
}

Since you don't specify the column name in the mapping, I guess the underlying ORM framework generates the column name "NAME" for it.

To resolve this error/warning, you could add the column name to the mapping

@Column( name = "name")

resulting in a lower-case column name or use upper-case notation in your dataset

<personnes NAME="toto"  pk="1" />

leaving the upper-case column name.

You need to set the following to true

DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES

within your DatabaseConfig object.

See org.dbunit.dataset.NoSuchTableException: Did not find table 'xxx' in schema 'null'

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