samedi 29 août 2015

JUnit-Tests for whole algorithm get in endless loop when testing mysql database

I am testing my project with JUnit. When i test the following for itself, everything is successfully visited (smoke test: Does everything run?).

Database (mySQL) also is cleared and recreated as wanted.

TestMethod:

@Test
public void testGetInstance() throws SQLException, InterruptedException {
    DatabaseConnection db = DatabaseConnection.getInstance();
    assertNotNull(db);

    //drops db so method is forced to recreate it.
    DatabaseDataSetter.dropDatabase();

    //test if relaunch on database is successful after recreation.
    db.connect();
    assertNotNull(db);
}

Method with endless loop:

/**
 * Clears complete database.
 * @throws SQLException 
 */
public static boolean dropDatabase() throws SQLException {

    PreparedStatement ps = null;

    try {
        ps = conn.prepareStatement("DROP DATABASE planning_data");
        ps.execute();
        ps.close();
        return true;
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }   
}

So why is it working when tested alone and not, if done in whole project? I also want to test other methods which mySQL-contact, but i don't think this is relevant, when it stops at the first occurence.

Aucun commentaire:

Enregistrer un commentaire