Preloader image

In this example, a REST service will be tested, using Application Composer and Mockito.

Application Composer is a testing API for TomEE. It’s faster to bootstrap than Arquillian. Mockito is a mocking framework. Both will be used for testing a REST service.


The REST service example is defined by the following class:

public class GreetingService {

    private Messager messager;

    public String message() {
        return messager.message();

A bean, messager, is injected. When a client accesses the resource GET /greeting, the content returned to the client will be the result of messager.message() method execution.

Messager functionality is defined by this interface:

public interface Messager {

    String message();

Testing the REST service

For testing this REST service, Application Composer will be used in combination with Mockito:

public class GreetingServiceTest {

    private Messager messager;

    public Properties configuration() {
        return new Properties() {{
            setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, Boolean.TRUE.toString());

    public Class<?> mockitoInjector() {
        return MockitoInjector.class;

    public Class<?>[] app() {
        return new Class<?>[]{GreetingService.class, Messager.class};

    public void checkMockIsUsed() throws IOException {

        final String message = IO.slurp(new URL("http://localhost:4204/GreetingServiceTest/greeting/"));
        assertEquals("mockito", message);

We can see the usage and configuration of ApplicationComposer. More information about it can be found here. An other interesting annotation is @MockInjector. This informs the container started by Application composer that Mockito is used, so mock proprieties will be injected.

The test method stubs the messager.message() method, returning the value "mockito". Then, it checks that by accessing GET greeting, the retrieved content is "mockito".

Full code example can be found here. It’s a maven project, and the tests can be run by the mvn clean install command.