Apache

The iPOJO documentation has moved

The new web site is here, update your bookmark.

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
  • Integration tests

Unitary tests are famous and were popularized with the proliferation of frameworks like Junit and TestNG. 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 (downloadable here). 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

Once every component has its unitary test suite (isn't it?), you can go further and creates integration test. In order to check the behavior of the system, iPOJO provides the junit4osgi framework. This framework allows the executing of Junit test inside OSGi. Refer to the Junit4OSGi page to know how to use it.

Moreover, OPS4J Pax Exam can also be used to test iPOJO components.



Last modified by clement.escoffier on 2009-12-27 09:05:12.0
Apache Felix, Felix, Apache, the Apache feather logo, and the Apache Felix project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.