Testing
Automated tests are a very useful way of improving code quality. They provide a context for development, documentation, and an on-going check of code correctness. These benefits give you the confidence to build on and refactor code knowing that it will continue to work as designed. This chapter covers the application testing facilities provided by Fabric3. Some familiarity with Ant, Maven, JUnit and mock objects are assumed.
Unit Testing
Java-based components in Fabric3 are in most cases simple POJOs with optional annotations. This means that this guide has little to add to the already well-covered topics of test-driven development and unit testing. It is important to note, however, that even with Fabric3's strong support for integration testing (examined below), it remains easier to find and fix a whole class of bugs by running lightweight tests as opposed to a (slower) test-harness.
Integration Testing
Fabric3 integration tests ensure that component implementations and composites provide the expected functionality and interact with other services and runtime resources in the expected way. They are referred to as 'itests'.
While Fabric3 does provide a lightweight standalone runtime environment, the recommended way to drive automated integration tests is through the specialized Gradle or Maven plugin. These handle the creation of an embedded Fabric3 runtime, deployment, and test execution. Fast runtime bootstrap and test execution makes iterative development easier. Since the Gradle and Maven plugins provide the same execution environment as the standalone server, component behavior verification can be done with little overhead. Use of the Gradle and Maven plugins also allows integration tests to be initiated in exactly the same way on development machines and the build server.
JUnit Tests
As detailed in the following sections, Fabric3 makes use of JUnit for writing in-container integration tests. Fabric3 provides a test runner that allows JUnit tests to act as components that can be injected with services to be test. When the build is run, the JUnit tests will be executed in a Fabric3 embedded container and their results reported as part of the test phase of the Maven or Gradle build. Â Writing a test component is simple - below is an example of how this is done:
@RunWith(Fabric3Runner.class) public class HelloWorldTest extends TestCase { @Reference protected HelloWorld helloWorld; public void testSayHello() { assertEquals("Hello, Foo", helloWorld.sayHello("Foo")); } }
Â