A Maven Integration Test

This example illustrates the use of Fabric3 ITest plugin for testing a simple service. The service will be injected into a JUnit test case and the test case will invoke the injected service in the ITest host environment. 

Setting Up the Code

The figure below shows the structure of the project:

/pom.xml
/src/main/java/HelloWorld.java
/src/main/java/HelloWorldImpl.java
/src/test/java/HelloWorldITest.java
/src/test/resources/itest.composite

The service interface and component implementation are as follows:

public interface HelloWorld {
    String sayHello(String name);
}

@Component
public class HelloWorldImpl implements HelloWorld {

   public String sayHello(String name) {
      return "Hello " + name;
   }

}

The test for the service is written as a standard JUnit test using the Fabric3Runner and placed under /src/test/java:

@RunWith(Fabric3Runner.class)
public class HelloWorldTest extends TestCase {

    @Reference 
    protected HelloWorld helloWorld;

    public void testSayHello() {
        assertEquals("Hello, Foo", helloWorld.sayHello("Foo"));
    }
}

When the Maven build is run, the HelloWorldImpl service will be deployed to an embedded Fabric3 container. The HelloWorldTest will be injected with a proxy to the service (which can be a local or remote wire depending on if a binding is configured) and run as part of the Maven test phase.   

The org.fabric3.api.implementation.junit.Fabric3Runner class is contained in the Fabric3 JUnit API jar, org.fabric3:fabric3-junit-api.

The Maven POM

The Maven POM must specify the Fabric3 or SCA API, Fabric3 JUnit API and JUnit as dependencies. It must also configure the Fabric3 ITest plugin:

<project ...>
    <dependencies>
        <dependency>
            <groupId>org.fabric3</groupId>
            <artifactId>fabric3-api</artifactId>
            <version>{fabric3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.fabric3</groupId>
            <artifactId>fabric3-junit-api</artifactId>
            <version>${fabric3.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>verify</defaultGoal>
        <plugins>
             <plugin>
                <groupId>org.fabric3</groupId>
                <artifactId>fabric3-itest-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Running the Tests

The tests are run executing mvn integration-test. This will boot the Fabric3 runtime, deploy the test composite, and call all test operations on configured JUnit components. Test results will then be collocated and reported. The following output will be generated when the plugin is run:

[INFO] [fabric3-itest:test {execution: default}]
[INFO] Starting Fabric3 Runtime ...
[INFO] JMX management extension installed
[INFO] JMX connector started on port 1199
[INFO] iTestContribution installed
[INFO] Composite {urn:fabric3.org}FunctionTestHarnessComposite deployed
[INFO] Executing tests...

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] Stopping Fabric3 {color:#831b92}Runtime{color} ...
[INFO] ----------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------
[INFO] Total time: 12 seconds

Default Repositories

By default, the iTest Plugin will attempt to resolve Fabric3 extension and profile dependencies not in the Maven Central Repository against the repository settings defined by the Fabric3 parent POM. If you use a repository manager and do not wish to have this behavior enabled, set the 'useDefaultRepositories' property to false:

<plugin>
   <groupId>org.codehaus.fabric3</groupId>
   <artifactId>fabric3-itest-plugin</artifactId>
   <version>${fabric3.version}</version>
   <configuration>
      <useDefaultRepositories>false</useDefaultRepositories>
      ..... 
   </configuration>
</plugin>