[Qt-jambi-interest] Strange Behavior whit Junit4 test suites and QJDBC

José Arcángel Salazar Delgado arcangel.salazar at gmail.com
Tue Jul 15 01:36:22 CEST 2008


Hello.

I had a trouble when I tried to run a test Suite whit Junit 4. When I 
ran the tests independently they work fine, but when I did in a test 
Suited they cannot connect to the DB. Here is a sample code:

//ProductoTest.java
public class ProductoTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        String[] args = new String[0];
        QCoreApplication.initialize(args);
        QJdbc.initialize();
       
        try {
            Class.forName("org.h2.Driver");
        }
        catch (Exception ex) {
            System.err.println(ex);
        }
        QSqlDatabase db = QSqlDatabase.addDatabase(QJdbc.ID);
        db.setDatabaseName("jdbc:h2:databases/productos/productos");
        db.setUserName("sa");
        db.setPassword("");
       
        if (db.open()) {
            System.out.println("Conexión realizada!");
        } else {
            System.err.println("Conexión fallida!");
            System.err.println(db.lastError().text());
        }
        return;
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        QSqlDatabase.database().close();
        QCoreApplication.exit();
        QCoreApplication.instance().dispose();
    }
    //Omit
}

//ClienteTest.java
public class ClienteTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        String[] args = new String[0];
        QCoreApplication.initialize(args);
        QJdbc.initialize();
       
        try {
            Class.forName("org.h2.Driver");
        }
        catch (Exception ex) {
            System.err.println(ex);
        }
        QSqlDatabase db = QSqlDatabase.addDatabase(QJdbc.ID);
        db.setDatabaseName("jdbc:h2:databases/clientes/clientes");
        db.setUserName("sa");
        db.setPassword("");
       
        if (db.open()) {
            System.out.println("Conexión realizada!");
        } else {
            System.err.println("Conexión fallida!");
            System.err.println(db.lastError().text());
        }
        return;
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        QSqlDatabase.database().close();
        QCoreApplication.exit();
        QCoreApplication.instance().dispose();
    }
    //Omit
}

//AllUnitTest.java
package com.linuxsoluciones.carniceria.models.test;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ClienteTest.class, ProductoTest.class})
public class AllUnitTest {

}

The ClienteTest runs fine, but ProductoTest is broken. This is the error:
QSqlDatabasePrivate::removeDatabase: connection 
'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabase: QJDBC driver not loaded
QSqlDatabase: available drivers:
Driver not loaded Driver not loaded

I don´t know why the QJDBC driver isn´t loaded. I think that is a 
problem of Junit (they don´t isolate the class test enough).
Is there any way to reload the QJDBC safety?

Thanks for any advice.

P.D: Is there any plan to port the QTest framework to Jambi?



More information about the Qt-jambi-interest mailing list