Running HTTP/REST Integration Tests efficiently in Eclipse

Home  >>  Eclipse  >>  Running HTTP/REST Integration Tests efficiently in Eclipse

Running HTTP/REST Integration Tests efficiently in Eclipse

On September 10, 2012, Posted by , In Eclipse,JUnit, By ,,,,, , With 8 Comments

Lately I had a chance to use the OSGi-JAX-RS-Connector library written by my dear fellow Holger Staudacher. The connector enables you to publish resources easily by registering @Path annotated types as OSGi services – which actually works quite nicely.

While it is natural for me to write the service classes test driven using plain JUnit tests it is also very important to provide additional integration tests. Those tests allow to check runtime availability and functionality of such services. To provide the latter I used another little helper written by Holger – restfuse which is a JUnit extension for automated HTTP/REST Tests.

The scenario looks somewhat like this:

A service

@Path( "/message" )
public class SampleService {
  
  @GET
  @Produces( MediaType.TEXT_PLAIN )
  public String getMessage() {
    return "Hello World";
  }
}

A JUnit test case

public class SampleServiceTest {
  
  @Test
  public void testGetMessage() {
    SampleService service = new SampleService();
    
    String message = service.getMessage();
    
    assertEquals( "Hello World", message );
  }
}

The service registration

<?xml version="1.0" encoding="UTF-8"?>
<scr:component
  xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
  name="SampleService">
   <implementation class="sample.SampleService"/>
   <service>
      <provide interface="sample.SampleService"/>
   </service>
</scr:component>

The restfuse integration test

@RunWith( HttpJUnitRunner.class )
public class SampleServiceHttpTest {
  
  @Rule
  public Destination destination
    = new Destination( "http://localhost:9092" );
  
  @Context
  private Response response;
  
  @HttpTest( method = Method.GET, path = "/services/message" )
  public void checkMessage() {
    String body = response.getBody( String.class );
    assertOk( response );
    assertEquals( MediaType.TEXT_PLAIN, response.getType() );
    assertEquals( "HelloWorld", body );
  }
}

The running service

While all of this was quite straight forward it bugged me somehow that running the integration tests locally required first to launch the server before I was able to execute the integration test. Preoccupied by the task at hand I often forgot to launch the server and ran into connection timeouts or the like.

But I found a solution for this by using a PDE JUnit launch configuration, because such a configuration can be setup to start the server within the process that runs the tests.

To do so create and select a test suite that contains all the integration tests to run1

…after that switch to the main tab and select the headless mode…

… and last but not least configure the program arguments used by the server which in our case basically concerns the port definition.

The bundle selection in the Plug-ins tab contains the same bundles as those of the osgi launch configuration that is used to run the server standalone plus the JUnit-, PDE JUnit-, restfuse-bundles and their dependencies. The selected test suite may looks like this:

@RunWith( Suite.class ) 
@SuiteClasses( {
  SampleServiceHttpTest.class
} )
public class AllRestApiIntegrationTestSuite {
  
  public static String BASE_URL
    = "http://localhost:" 
    + System.getProperty( "org.osgi.service.http.port" );
}

The only unusual thing here is the BASE_URL constant definition. As mentioned above the server port of the test run is specified as a program argument in the launch configuration. But restfuse tests need to provide the port during the destination rule definition. Using the approach above allows to change the port in the configuration without affecting the tests. Simply use the constant as parameter in the definition as shown in the following snippet23.

 @Rule
  public Destination destination
    = new Destination( BASE_URL );

This simple setup worked out very well and improved my workflow of running the integration tests locally. And saving the launch configuration in a shared project easily enables your team mates to reuse it.

So this is it for today and as always feedback is highly appreciated. By the way, Holger promised me to write a post about how to integrate that stuff described above into a maven/tycho based build4 – so stay tuned :-)


  1. Of course you can also use the possibility of running all tests of the selected project, package or source folder – but for our purposes here using the suite approach and running a single test case is quite ok
  2. You probably would provide a separate class for the constant definition in a real world scenario to avoid the coupling of the tests to the suite. I skipped this here for simplification.
  3. Note that the BASE_URL is included using static imports for better readability of the snippet
  4. Holger kept his promise, see: http://eclipsesource.com/blogs/2012/09/11/running-httprest-integration-tests-in-an-eclipse-tycho-build/
Follow me

Frank Appel

Frank is a stalwart of agile methods and test driven development in particular. He understands software development as a craftsmanship based on a well-balanced mix of knowledge and the experience of the daily work.

fappel@codeaffine.com
Follow me

Latest posts by Frank Appel (see all)

8 Comments so far:

  1. [...] Ejecutando tests de integración HTTP/REST de un modo eficiente en Eclipse [...]

  2. rahul says:

    Can you write an example of POST service using restfuse.

    • Frank Appel says:

      I did not run it but basically it should look something like the following snippet:

      Simple Post Service:

      @Path( "/resources" )
      public class ResourceService {
          
        @POST
        @Path( "/names" )
        public void addName( String name ) {
        }
      }
      

      According Restfuse Test:

      @RunWith( HttpJUnitRunner.class )
      public class ResourceServiceHttpTest {
        @Rule
        public Destination destination
          = new Destination( "http://localhost:9092" );
      
        @Context
        private Response response;
      
        @HttpTest( method = Method.POST,
                   path = "/services/resources/names" )
        public void testAddName() {
          assertOk( response );
        }
      }
      
  3. Govinda Attal says:

    restfuse really looks impressive… looking for near to real examples so that can quickly decide if we can recommend this within the organisation… Could you please provide an example of post request where the input message preferably an object is provided as JSON and/or XML and similarly response message is parsed…. that will be great….

    If it is possible for you to share a link which provides few concrete examples it will be greatly appreciated… :-)

    Many Thanks,
    Govinda

  4. Holger says:

    Hi Govinda,
    I have created gist that shows how to send a post request with json content.
    https://gist.github.com/hstaudacher/4994161

    Hope this helps.

    Cheers Holger

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>