A Gradle Integration Test

This example illustrates the use of Fabric3 Gradle 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. 

Setting Up the Code

The figure below shows the structure of the project:

/build.gradle
/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 Gradle 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 Gradle Build File

The Gradle build must specify the SCA or Fabric3 API, Fabric3 JUnit API and JUnit as dependencies. It must also configure the Fabric3 plugin:

apply plugin: 'fabric3-test'

compile group: fabric3Group, name: 'fabric3-api', version: fabric3Version
 
testCompile group: 'junit', name: 'junit', version: junitVersion
testCompile group: fabric3Group, name: 'fabric3-junit-api', version: fabric3Version
 
buildscript {
	dependencies {
    	classpath group: 'org.fabric3.gradle', name: 'fabric3-test', version: fabric3Version
    }
}
 
fabric3Test {

    report true;
	
}

 

The tests are run executing the fabric3Test task. 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.Â