Is there any way for DBUnit to automatically create tables?(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 有没有办法自动创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:DBUnit 有没有办法自动创建表?
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01