Easy testing with embedded openejb and hsql

In my current project I happened to question the need to install and set up a DBMS and an EJB container just to be able to run automatic tests during development. Luckily I found out that both openejb and hsql can be "embedded" i.e. used in-process with no per-workspace configuration.

Now a new developer needs just pull the stuff from CVS and run the tests and they pass. No installation and no configuration, just Plain Old Coding TM :)

Here is a quick HOWTO:

1 Make a stripped down "installation" of openejb. The full installation works ok, too, but it's quite slow to start:

2 Add all openejb jars (dist/* and lib/*) and hsql.jar to classpath

3 Make your build script create the ejb jar under build/deploy

Now you can use the embedded openejb by creating the initial context like this:

        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.openejb.client.LocalInitialContextFactory");
        // here "../your-ejb-project" makes this test work in any project:
        System.setProperty("openejb.home",
                "../your-ejb-project/tools/openejb");

        InitialContext ctx = new InitialContext(properties);

The embedded volatile hsql DBMS can be used by creating the connection like this:

        Class.forName("org.hsqldb.jdbcDriver");
        Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:aname",
                "sa", "");

Originally published on 2005-05-03 at http://www.jroller.com/wipu/entry/easy_testing_with_embedded_openejb under category Java