DBUnit 有没有办法自动创建表? [英] Is there any way for DBUnit to automatically create tables?

查看:43
本文介绍了DBUnit 有没有办法自动创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到 DBUnit 不会自行创建表(请参阅 如何使用带有普通 JDBC 和 HSQLDB 的 DBUnit 进行测试而不会遇到 NoSuchTableException?).

DBUnit 有没有办法自动从数据集或 dtd 创建表?

对于像 HSQLDB 这样的内存数据库的简单测试,可以使用粗略的方法来自动创建表:

private void createHsqldbTables(IDataSet dataSet, Connection connection) throws DataSetException, SQLException {String[] tableNames = dataSet.getTableNames();字符串 sql = "";对于(字符串表名:表名){ITable 表 = dataSet.getTable(tableName);ITableMetaData 元数据 = table.getTableMetaData();Column[] 列 = metadata.getColumns();sql += "创建表" + 表名 + "( ";布尔优先=真;对于(列列:列){如果(!第一){sql += ", ";}String columnName = column.getColumnName();字符串类型 = resolveType((String) table.getValue(0, columnName));sql += 列名 + " " + 类型;如果(第一){sql += "主键";第一=假;}}sql += "); ";}PreparedStatement pp = connection.prepareStatement(sql);pp.executeUpdate();}私有字符串解析类型(字符串 str){尝试 {if (new Double(str).toString().equals(str)) {返回双";}if (new Integer(str).toString().equals(str)) {返回int";}} 捕捉(异常 e){}返回varchar";}

解决方案

不是.正如您链接的答案所指出的,dbunit xml 文件包含数据,但不包含列类型.

而且您真的不想这样做;您可能会冒着测试工件污染数据库的风险,从而导致生产代码意外依赖测试过程创建的表的可能性.p>

需要这样做强烈表明您没有充分定义和编写数据库创建和维护过程.

I just realized that DBUnit doesn't create tables by itself (see How do I test with DBUnit with plain JDBC and HSQLDB without facing a NoSuchTableException?).

Is there any way for DBUnit to automatically create tables from a dataset or dtd?

EDIT: For simple testing of an in-memory database like HSQLDB, a crude approach can be used to automatically create tables:

private void createHsqldbTables(IDataSet dataSet, Connection connection) throws DataSetException, SQLException {
    String[] tableNames = dataSet.getTableNames();

    String sql = "";
    for (String tableName : tableNames) {
      ITable table = dataSet.getTable(tableName);
      ITableMetaData metadata = table.getTableMetaData();
      Column[] columns = metadata.getColumns();

      sql += "create table " + tableName + "( ";
      boolean first = true;
      for (Column column : columns) {
        if (!first) {
          sql += ", ";
        }
        String columnName = column.getColumnName();
        String type = resolveType((String) table.getValue(0, columnName));
        sql += columnName + " " + type;
        if (first) {
          sql += " primary key";
          first = false;
        }
      }
      sql += "); ";
    }
    PreparedStatement pp = connection.prepareStatement(sql);
    pp.executeUpdate();
}

private String resolveType(String str) {
  try {
    if (new Double(str).toString().equals(str)) {
      return "double";
    }
    if (new Integer(str).toString().equals(str)) {
      return "int";
    }
  } catch (Exception e) {}

  return "varchar";
}

解决方案

Not really. As the answer you linked points out, the dbunit xml files contain data, but not column types.

And you really don't want to do this; you risk polluting your database with test artifacts, opening up the possibility that production code will accidentally rely on tables created by the test process.

Needing to do this strongly suggests you don't have your db creation and maintenance process adequately defined and scripted.

这篇关于DBUnit 有没有办法自动创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆