Application Composer Getting Started

ApplicationComposer API is mainly contained in org.apache.openejb.testing package (historically, today we would have called the package org.apache.tomee.applicationcomposer).


To start using ApplicationComposer you need to add some dependencies.

The minimum required one is openejb-core:


If you need JAXRS services you'll add (or replace thanks to transitivity of maven) openejb-cxf-rs:


If you need JAXWS services you'll add (or replace thanks to transitivity of maven) openejb-cxf:



ApplicationComposer Components


An ApplicationComposer needs at minimum a module (the application you need to deploy).

To do so you have two cases:

The expected returned type of these methods are in org.apache.openejb.jee package:

Note that for easiness @Classes was added to be able to describe a module and some scanned classes. For instance the following snippet will create a web application with classes C1, C2 as CDI beans and E1 as an EJB automatically:

@Classes(cdi = true, value = { C1.class, C2.class, E1.class })
public WebApp app() {
    return new WebApp();


Often you need to customize a bit the container or at least create some resources like test databases. To do so you can create a method returning Properties which will be the container properties.

Note: to simplify writing properties you can use PropertiesBuilder util class which is just a fluent API to write properties.

In these properties you can reuse OpenEJB/TomEE property syntax for resources.

Here is a sample:

public Properties configuration() {
    return new PropertiesBuilder()
        .p("db", "new://Resource?type=DataSource")
        .p("db.JdbcUrld", "jdbc:hsqldb:mem:test")

Since TomEE 2.x you can also put properties on ApplicationComposer class using @ContainerProperties API:

  @ContainerProperties.Property(name = "db", value = "new://Resource?type=DataSource"),
  @ContainerProperties.Property(name = "db.JdbcUrl", value = "jdbc:hsqldb:mem:test")
public class MyAppComposer() {
  // ...


Sometimes you need to customize a container component. The most common use case is the security service to mock a little bit authorization if you don't care in your test.

To do so just write a method decorated with @Component returning the instance you desire.

Components in TomEE are stored in a container Map and the key needs to be a Class. This one is deduced from the returned type of the @Component method:

public SecurityService mockSecurity() {
    return new MySecurityService();

How to run it?


If you use JUnit you have mainly 2 solutions to run you "model" using the ApplicationComposer:

Tip: since TomEE 2.x ApplicationComposerRule is decomposed in 2 rules if you need: ContainerRule and DeployApplication. Using JUnit RuleChain you can chain them to get the samebehavior as ApplicationComposerRule or better deploy multiple ApplicationComposer models and controlling their deployment ordering (to mock a remote service for instance).

Finally just write @Test method using test class injections as if the test class was a managed bean!


TestNG integration is quite simple today and mainly ApplicationComposerListener class you can configure as a listener to get ApplicationComposer features.

Finally just write TestNG @Test method using test class injections as if the test class was a managed bean!


Since TomEE 2.x you can also use ApplicationComposers to directly run you ApplicationComposer model as a standalone application:

public class MyApp {
    public static void main(String[] args) {, args);

    // @Module, @Configuration etc...

Tip: if MyApp has @PostConstruct methods they will be respected and if MyApp has a constructor taking an array of String it will be instantiated getting the second parameter as argument (ie you can propagate your main parameter to your model to modify your application depending it!)

JUnit Sample

@Classes(cdi = true, value = { MyService.class, MyOtherService.class })
@ContainerProperties(@ContainerProperties.Property(name = "myDb", value = "new://Resource?type=DataSource"))
public class MyTest {
    @Resource(name = "myDb")
    private DataSource ds;

    private MyService service;

    public void myTest() {
        // do test using injections

Going further

If you want to learn more about ApplicationComposer see Advanced page.