How to test iPOJO Components

Testing components is a required step in the application development process. This page explains strategies to test iPOJO-based components and applications.

Introduction

Basically, we can distinguish 2 types of tests:

Unitary tests are famous and were popularized with the proliferation of frameworks like Junit and [TestNG|http://testng.org/doc/]. These tests check the behavior of classes. More specially, these tests check the method results according to different parameter values. These test are executed outside the execution environment, can use mock object (link) to replace/simulate missing dependencies...

Integration tests focus on the execution of the application inside the execution environment. It checks the global behavior of a service or an application. In the case of iPOJO, those tests are executed on OSGi. Some dependencies can be also replaced by mock services/objects.

Executing unitary tests on iPOJO components

Unitary tests allow checking the internal behavior of an iPOJO component. Those tests ignore the injected dependencies and focus only on the business code of the component.

To test components, you can use Junit. A special constructor setting fields can do the dependency injection. These tests are executed before the bundlelization.

Let's imagine a maven project. Unitary tests are placed in the src/test/java folder. You can use mock object as illustrated in the example. When you launch the mvn clean install command, maven compiles your classes and executes the tests. If the tests are executed successfully, the build process continues and creates the bundle as follows:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Dummy Component
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory /Users/clement/Documents/workspaces/ipojo-dev/UnitaryTest/target
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to /Users/clement/Documents/
        workspaces/ipojo-dev/UnitaryTest/target/classes
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to /Users/clement/Documents/
        workspaces/ipojo-dev/UnitaryTest/target/test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: /Users/clement/Documents/
        workspaces/ipojo-dev/UnitaryTest/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.apache.felix.ipojo.ut.component.test.MyTestCase
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.076 sec

Results :

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

[INFO] [bundle:bundle]
[INFO] [ipojo:ipojo-bundle {execution: default}]
[INFO] Start bundle manipulation
[INFO] Metadata file : /Users/clement/Documents/
  workspaces/ipojo-dev/UnitaryTest/target/classes/metadata.xml
[INFO] Input Bundle File : /Users/clement/Documents/workspaces/
  ipojo-dev/UnitaryTest/target/org.apache.felix.ipojo.test.dummy.component-1.1.0-SNAPSHOT.jar
[INFO] Bundle manipulation - SUCCESS
[INFO] [install:install]
[INFO] Installing /Users/clement/Documents/workspaces/
  ipojo-dev/UnitaryTest/target/org.apache.felix.ipojo.test.dummy.component-1.1.0-SNAPSHOT.jar 
  to /Users/clement/.m2/repository/ipojo/tests/org.apache.felix.ipojo.test.dummy.component/1.1.0-SNAPSHOT
  /org.apache.felix.ipojo.test.dummy.component-1.1.0-SNAPSHOT.jar
[INFO] [bundle:install]
[INFO] Parsing file:/Users/clement/.m2/repository/repository.xml
[INFO] Installing ipojo/tests/org.apache.felix.ipojo.test.dummy.component/
    1.1.0-SNAPSHOT/org.apache.felix.ipojo.test.dummy.component-1.1.0-SNAPSHOT.jar
[INFO] Writing OBR metadata
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Tue Oct 21 14:03:03 CEST 2008
[INFO] Final Memory: 11M/25M
[INFO] ------------------------------------------------------------------------

Executing integration tests

We recommend to use Pax Exam 3 to test iPOJO components. More details about pax exam 3 are available here.