简体   繁体   English

Unitils / DBunit和数据库测试

[英]Unitils/DBunit and database test

I want to try to make unit test with DBUnit but I have a problem with my dataset. 我想尝试使用DBUnit进行单元测试,但我的数据集有问题。

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". 我不明白为什么dbunit搜索列“NAME”而我的列是“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 在Unitils 3.4.1中,他们添加了一个新属性org.dbunit.database.IMetadataHandler.implClassName

In my unitils.properties file, I added the following line 在我的unitils.properties文件中,我添加了以下行

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. 是的,我知道,根据Unitils的网站,没有版本3.4.1,但你可以通过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 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. 在测试类中实现方法setUpDatabaseConfig并设置工厂。

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. 由于您没有在映射中指定列名,我猜基础ORM框架会为其生成列名“NAME”。

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 您需要将以下内容设置为true

DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES

within your DatabaseConfig object. DatabaseConfig对象中。

See org.dbunit.dataset.NoSuchTableException: Did not find table 'xxx' in schema 'null' 请参阅org.dbunit.dataset.NoSuchTableException:未在架构中找到表'xxx''null'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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