ApplicationComposer (OpenEJB) configuration


ApplicationComposer is a nice OpenEJB runner to write simple but very fast JavaEE tests.

Here what a common ApplicationComposer test looks like:

@RunWith(ApplicationComposer.class)
public class MyTest {
    @Module
    public Class<?>[] bean() {
        // return classes
    }

    @Inject // get injected beans as in a managed bean
    private MyBean bean;

    @Test // write tests as usual
    public void myTest() throws NamingException {
        // ...
    }
}

This is great but for more real cases you need to provide your datasources/resources configuration.

Until now it was done through @Configuration method returning Properties:

@RunWith(ApplicationComposer.class)
public class MyTest {
    @Configuration
    public Properties configuration() {
        final Properties props = new Properties();
        props.setProperty("ds", "new://Resource?type="DataSource");
        // set driver, url...
        return props;
    }

    // ...
}

This is great but when you already have an openejb.xml (or a tomee.xml) and use it as base it is quite complicated to convert it to properties. You can of course convert your openejb.xml to properties set in conf/system.properties and use it in your tests and production environment but it is not that common and depending on people properties to configure such things is not as readable as our xml format.

That’s why you can now return directly your Openejb configuration or if you prefer just the path of the file (search in the classpath):

@RunWith(ApplicationComposer.class)
public class MyTest {
    @Configuration
    public String configuration() {
        return "my-openejb.xml";
    }

    // ...
}

or with Openejb (can handle file and not only classpath resources):

@RunWith(ApplicationComposer.class)
public class MyTest {
    @Configuration
    public Openejb configuration() {
        return  JaxbOpenejb.readConfig("file:my-openejb.xml");
    }

    // ...
}

In real projects it can save some test logic and help you test your app faster ;).

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s