Running HTTP/REST Integration tests in an Eclipse Tycho Build

September 11, 2012 | 3 min Read

Editor’s note: the Restfuse tool is no longer maintained and has been archived. However, you can still access the sources on GitHub.

A few days ago my fellow Frank wrote about running HTTP/REST Integration Tests efficiently in Eclipse. Today I want to show you how to embed those tests in a headless build using Eclipse Tycho. Embedding restfuse in a build is as simple as embedding plain JUnit tests. But when trying to start an OSGi server and test against this server as Frank described things are little bit more complicated.

Our scenario is identical to Frank’s. We publish REST Services within an OSGi instance using the OSGi-JAX-RS Connector. This is optional - you can publish the services any way you prefer. Our requirements are that before the tests run, an OSGi instance needs to start, the services should be published and the tests should run against the services afterwards. Using Tycho this is basically an easy task because Tycho has the eclipse-test-plugin packaging type doing most of the required things.

To follow the steps below, you can download the source from github: https://github.com/eclipsesource/restfuse-tycho-integration

In our scenario we have a bundle called com.eclipsesource.restfuse.example.service. This bundle publishes a REST service using the OSGI-JAX-RS Connector by registering @Path annotated services. Our restfuse integration tests are located in the com.eclipsesource.restfuse.example.test bundle (not a fragment in this case, but it doesn’t really matter). The parent pom of our build sits in the project com.eclipsesource.restfuse.example.build. The parent pom has one section to note which is the repositories section. In order to run restfuse tests, the restfuse p2 repository needs to be added here.


  
    juno
    p2
    https://download.eclipse.org/releases/juno
  
  
    osgi-jaxrs
    p2
    https://hstaudacher.github.com/osgi-jax-rs-connector
  
  
    restfuse
    p2
    https://download.eclipsesource.com/technology/restfuse/p2
  

The most interesting part is in the build section of the test plugin pom file. Here the OSGi environment needs to be specified. The eclipse-test-plugin packaging type can resolve bundle dependencies automatically. But when using implicit dependencies you need to put a little more work into the build section. In our example we have three implicit dependencies. These are dependencies to org.eclipse.equinox.ds, com.eclipsesource.osgi.jaxrs.connector and to the Jetty HttpService implementation.

Another “not so” obvious dependency exists in our com.eclipsesource.restfuse.example.test bundle. Because we are writing integration tests to test the HTTP interface, we don’t have any hard dependency on the code sitting in the application bundle (com.eclipsesource.restfuse.example.service). Nevertheless, we need to add a dependency to this bundle to enable Tycho to resolve it for the tests.

After the dependencies, we need to configure the OSGi instance. This means we need to start bundles, set start levels and set properties. In our example we need to start the bundles we have implicit dependencies on. Next, we need to start a fourth bundle explicitly, that is, our application bundle. The property we need to set is the port for the HttpService (org.osgi.service.http.port). As Frank described in his blog post, this property will be also used to get the port for the Destination @Rule. The full build section of the test plugin looks like this:


  
    
      ${tycho-groupid}
      tycho-surefire-plugin
      ${tycho-version}
      
        -Dorg.osgi.service.http.port=11042
        true
        
          
            org.eclipse.equinox.ds
            1
            true
          
          
            org.eclipse.equinox.http.jetty
            2
            true
          
          
            com.eclipsesource.jaxrs.connector
            3
            true
          
          
          
            com.eclipsesource.restfuse.example.service
            4
            true
          
        
        
          
            p2-installable-unit
            org.eclipse.equinox.ds
            1.4.0
          
          
            eclipse-feature
            org.eclipse.equinox.core.feature
            1.1.0
          
          
            eclipse-feature
            org.eclipse.equinox.compendium.sdk
            3.8.0
          
          
            eclipse-feature
            org.eclipse.equinox.server.jetty
            1.1.0
          
          
            p2-installable-unit
            com.eclipsesource.jaxrs.connector
            2.0.0
          
        
      
    
  

You’ll notice that in addition to the implicit dependencies we also added dependencies to org.eclipse.equinox.core.featureorg.eclipse.equinox.compendium.sdk and org.eclipse.equinox.server.jetty to satisfy the runtime dependencies.

To launch the build you can use the Eclipse m2e tooling using the launch config located in com.eclipsesource.restfuse.example.build or simply run mvn clean verify from the same directory. After the tests complete the test reports will be in com.eclipsesource.restfuse.example.test/target/surefire-reports.

It took me a while to figure out this setup and I hope this blog will save you a little time. If you have setups for other build systems I would really love to see them. As always, feel free to disagree ;)

Follow @hstaudacher