java - 数据库单元 : NoSuchTableException caught

标签 java testing db2 dbunit

过去 2 天我一直在使用 DBUnit,我在比较表格和 xml 文件时遇到了问题。

我粘贴了所有代码并简化了一些代码,以便人们可以尝试找出问题所在。

实际上,执行提取(从 DB2)的方法效果很好。我只是添加了一个与提取非常相似的方法,但它的功能是将表与 XML 文件进行比较。在 Debug模式下,当我到达这一行时:ITable tableAComparer = dataSet.getTable(table.getSchema().concat(".").concat(table.getTableName()));, 我得到一个 NoSuchTableException,我不明白为什么...

有没有人有提示?

谢谢。

public abstract class TestNonRegressionAbstract extends DBTestCase {

private IDatabaseConnection   dbConn;
private Config                cfg;
private ArrayList<Table> tablesToExtract = new ArrayList<Table>();
private String path = null;

/**
 * Methode permettant de cleaner la bd avant le chargement
 * 
 * @see org.dbunit.DatabaseTestCase#setUp()
 */
protected void setUp() throws Exception {
    path = new java.io.File("").getAbsolutePath().concat("/junit/");
}

/**
 * 
 * Méthode qui extrait les tables qui doivent être backupées avant les tests
 * 
 * @throws Exception si une exception est lancée
 */
protected void extractTables() throws Exception {

    try {
        for (Table table : tablesToExtract) {
            dbConn = initDBUnitConnection(table.getSchema());
            QueryDataSet dataSet = new QueryDataSet(dbConn);
            dataSet.addTable(table.getSchema().concat(".").concat(table.getTableName()));
            FlatXmlDataSet.write(dataSet, new FileOutputStream(path + table.getNomFichier()));
            dbConn.close();
            dbConn = null;
        }
    } finally {
        if (dbConn != null)
        dbConn.close();
        dbConn = null;
    }
}

/**
 * Méthode qui compare une table de la bd avec un fichier xml contenant les valeurs de la table 
 * avant l'exécution du test.
 * 
 * @throws Exception l'exception.
 */
protected void compareTables() throws Exception {

    this.getTablesToExtract().add(new Table("PRM", "TCALENDRIER", "PRM_TCALENDRIER.XML"));
    try {
        for (Table table : tablesToExtract) {
            DiffCollectingFailureHandler myHandler = new DiffCollectingFailureHandler();
            dbConn = initDBUnitConnection(table.getSchema());
            dbConn.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

            QueryDataSet dataSet = new QueryDataSet(dbConn);
            ITable tableAComparer = dataSet.getTable(table.getSchema().concat(".").concat(table.getTableName()));

            ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(tableAComparer, new String[]{"MAJPAR", "MAJTIMESTAMP"});
            IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new File(path + table.getNomFichier()));
            Assertion.assertEquals(expectedDataSet.getTable(table.getTableName()), filteredTable, myHandler);

            @SuppressWarnings("unchecked")
            List<Difference> diffList = myHandler.getDiffList();

            for (Difference difference : diffList) {
                System.out.println("Différence trouvé :" + difference.getColumnName() 
                        +  " Actuel:" + difference.getActualTable() +  " Attendu:" + difference.getExpectedValue());
            }
            dbConn.close();
            dbConn = null;
        }
    } finally {
        if (dbConn != null)
            dbConn.close();
            dbConn = null;
    }
}

/**
 * Méthode qui initialise la connection DBUnit
 * 
 * @param cfg
 *            la configuration avec les param BD
 * @return La connection
 * @throws Exception
 *             si erreur
 */
private IDatabaseConnection initDBUnitConnection(String schema) throws Exception {
    if (dbConn == null) {
        if (cfg == null) {
            initConfig();
        }
        String driver = cfg.getValue("CRP_DB_DRIVER");
        String dbName = cfg.getValue("CRP_DB_NAME");
        String dbUser = cfg.getValue("CRP_DB_USER");
        String dbPswd = cfg.getValue("CRP_DB_PASSWORD");
        Class.forName(driver);
        Connection connDBDemande = DriverManager.getConnection(dbName, dbUser, dbPswd);

        // Init DBUnit connection
        //dbConn = new DatabaseConnection(connDBDemande);
        dbConn = new DatabaseConnection(connDBDemande, schema);
    }
    return dbConn;
}

private void initConfig() throws Exception {
    if (cfg == null) {
        cfg = new Config("com/foo/bar/junit/nonregression/CRPTest.properties");
    }
}

/**
 * @see org.dbunit.DatabaseTestCase#getDataSet()
 */
@Override
protected IDataSet getDataSet() throws Exception {
    IDataSet databaseDataSet = getDbConn().createDataSet();
    return databaseDataSet; 
}

/**
 * 
 * Méthode qui récupère la table
 * 
 * @param table le nom de la table à récuperer
 * @return la table
 * @throws Exception si une exception survient
 */
protected ITable getDataFromTable(String table) throws Exception {

    ITable actualTable = getDataSet().getTable(table);
    return actualTable; 
}

/**
 * 
 * Méthode qui retourne la bd via DBUnit
 * 
 * @return la connection à la BD via DBUnit
 */
public IDatabaseConnection getDbConn() {
    return this.dbConn;
}


public void setDbConn(IDatabaseConnection aDbConn) {
    this.dbConn = aDbConn;
}

public class Table {

    public Table(String schema, String tableName, String nomFichier) {
        this.schema = schema;
        this.tableName = tableName;
        this.nomFichier = nomFichier;
    }

    private String schema;
    private String tableName;
    private String nomFichier;
    public String getSchema() {
        return this.schema;
    }
    public void setSchema(String aSchema) {
        this.schema = aSchema;
    }
    public String getTableName() {
        return this.tableName;
    }
    public void setTableName(String aTableName) {
        this.tableName = aTableName;
    }
    public String getNomFichier() {
        return this.nomFichier;
    }
    public void setNomFichier(String aNomFichier) {
        this.nomFichier = aNomFichier;
    }

}

public ArrayList<Table> getTablesToExtract() {
    return this.tablesToExtract;
}

public void setTablesToExtract(ArrayList<Table> aTablesToExtract) {
    this.tablesToExtract = aTablesToExtract;
}

最佳答案

终于找到了我的问题的答案:

首先,我需要通过这种方式将表添加到 QueryDataSet 对象 (dataSet)。

dataSet.addTable(table.getSchema().concat(".").concat(table.getTableName()), table.getSelectStatement());

并通过添加 SELECT 语句来添加查询参数。

关于java - 数据库单元 : NoSuchTableException caught,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9402124/

相关文章:

testing - Kotlin 测试类应该是内部的吗?

node.js - 将我的应用程序部署到 bluemix 时获取 "failed to start accepting connection"

java - 在多台机器上运行调度程序

db2 - 基于 db2 中的 ID 匹配,从一个表到另一个表的 SQL 更新

java - 避免 for 循环并尝试使用集合 API(性能)

java - Hibernate:无法检索关联的列表

java - Mybatis获取clob字段

java - selenium如何继续往下一页填充数据?

selenium - 在 Selenium Webdriver 中通过 xpath 查找元素或通过 css 查找元素哪个更好?

testing - 当我们可以做一个委托(delegate)使其可测试时,为什么要提取一个接口(interface)?