Integration Test with OSGi, Spring DM, and Maven

Status

Introduction

From Cytoscape 3, all modules are OSGi bundles. This means unit tests are not enough to verify the bundle is actually working or not on the OSGi framework because unit tests does not validate configuration files for Spring. To test the bundle's functionality, you need to write an integration test.

Automate Integration Test

You can integrate integration test to your build process by Maven. The following is the step-by-step instruction how to write an integration test for implementation bundles, which are actually import/export OSGi services.

Pom File

Integration test should be executed AFTER your code passes the unit tests. To run separate test suite as an integration test, you need to use maven-failsafe-plugin. You need to add the following section to your pom file:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>integration-test</id>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
          <execution>
            <id>verify</id>
            <goals>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Log4j Props

It is very hard to understand what's going on in the test process without watching log from Spring Extender. To watch log messages from Spring, you need to add log4j.properties file in src/test/resources/:

log4j.rootCategory=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout.ConversionPattern=%t %p [%c] - %m%n
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.threshold=TRACE

log4j.logger.org.springframework=INFO
log4j.logger.org.springframework.osgi.test=INFO

Test Code

Integration test code is very similar to standard JUnit test cases. To use testing framework by Spring, you need to use AbstractConfigurableBundleCreatorTests

   1 // Use IT*.java for Integration tests
   2 public class ITViewModelImpl extends AbstractConfigurableBundleCreatorTests {
   3     // Your test code
   4 }
   5 

The test class file should start with IT. Complete example code is available here:

http://chianti.ucsd.edu/svn/core3/viewmodel-impl/trunk/src/test/java/org/cytoscape/viewmodel/ITViewModelImpl.java

To learn more about the Spring's testing framework, please read this document:

http://static.springsource.org/osgi/docs/current/reference/html/testing.html

Configuration File for Testing

In the test, you need to import services from the target bundle and check it's actually working or not. In the following example, integration test imports two OSGi services exported by viewmodel-impl:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                      http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.0.xsd"
        default-lazy-init="false">

        <!-- Import services for testing -->
        <osgi:reference id="rootVisualLexiconServiceRef"
                interface="org.cytoscape.view.model.RootVisualLexicon" />
        <osgi:reference id="cyNetworkViewFactoryServiceRef"
                interface="org.cytoscape.view.model.CyNetworkViewFactory" />

</beans>

Example project with integration test is available here:

http://chianti.ucsd.edu/svn/core3/viewmodel-impl/trunk/

Outdated_Cytoscape_3.0/Developer/Testing/Integration (last edited 2011-02-24 15:35:52 by PietMolenaar)

Funding for Cytoscape is provided by a federal grant from the U.S. National Institute of General Medical Sciences (NIGMS) of the Na tional Institutes of Health (NIH) under award number GM070743-01. Corporate funding is provided through a contract from Unilever PLC.

MoinMoin Appliance - Powered by TurnKey Linux