I set up the HSQL database in the JUnit TestSetup.  This drops and re-creates 
the database for each test class that need the database, but the overhead is 
quite low, only a few test classes need the database running, and it ensures 
left over database artifacts will not have any side-effects on future tests.  
Only starting the database and creating the tables once for all tests can be 
easily accomplished by keeping around the static connection and have all test 
classes get that connection instead of doing the setup themselves.

The code looks something like this:

public class JDBCTest extends TestCase {
  private static Connection conn;

  public static Test suite() {
    TestSuite suite = new TestSuite(JDBCTest.class);

    TestSetup wrapper = new TestSetup(suite) {
      protected void setUp()
        throws Exception {
        Class.forName("org.hsqldb.jdbcDriver");
        conn = DriverManager.getConnection("jdbc:hsqldb:.", "sa", "");
        // LOAD SCHEMA HERE
      }
      protected void tearDown()
        throws Exception {
          conn.close();
      }
    };
    return wrapper;
  }

  public void testSomething() {
  }
}

I use a file-based db by replacing "jdbc:hsqldb:." with something like 
"jdbc:hsqldb:tmpdb" only if I need to view the actual tables or data in the 
database.

-jake


On Wednesday 05 January 2005 11:35 am, Charles N. Harvey III wrote:
> Hello.
> I have been doing lots of reading about how great it is to use an in-process
> DB like Hypersonic for testing.  Thing is, there are not many examples of
> how to do this.  What I want to do is start an in-memory DB (not a file DB
> because the test is small) when I run my tests, and then run tests.
> 
> So the first thing that has to happen when I run "maven test" is that the
> DB has to be created.  Then I have to execute CREATE statements for the
> necessary tables.  Then I can proceed with my tests.
> 
> Has anyone done this?  Should I use a file-based DB instead of in memory?
> How do I get the maven test suite to "do this one thing first, and only
> once"?
> 
> Any help is greatly appreciated.
> 
> 
> Charlie
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to