Jump to: navigation, search

Gyrex/Developer Guide/JUnit

Gyrex supports and recommends writing tests with JUnit. Any JUnit tests must be run as JUnit OSGi plug-in tests in headless mode. Both, PDE Build and Tycho support such execution of tests. In order to assist developers with running and developing tests, Gyrex provides a few helpers for JUnit.

Dependencies

  • Bundle org.eclipse.gyrex.junit (Gyrex 1.2 or higher)
  • JUnit (4.10 or higher)

Integration Tests

Some tests may require a full Gyrex environment to be bootstrapped and running. We call those Integration Tests as they test a certain functionality in the system while operating/integrating with other essential services of the platform (for example, the cloud services backed by ZooKeeper).

Writing integration tests with Gyrex should be done by using the JUnit rules. JUnit rules allow defining dependencies/requirements for JUnit tests. Gyrex provides a rule which ensures that a Gyrex environment is bootstrapped and started before a test is run. Consequently, the server will be stopped after a test is finished. It’s recommended to implement dependencies as a @ClassRule inside a JUnit suite. This groups integration tests together and start the server only once and stop it after executing all tests.

Bundle Activation

Bundle activation is important when writing tests. Basically, you can not assume that a Gyrex server environment is fully bootstrapped and started in any bundle activator. This shouldn't be done anyway because OSGi is a dynamic environment and bundles can come and go at any time. Thus, make sure that you do not execute any logic during bundle activation which can fail if a Gyrex server environment is not available.

Instead, it's recommended to use Declarative Services or Gyrex Server Roles to trigger lazy activation once an environment is ready.

Example

The following example defines a JUnit suite for running integration test.

import org.eclipse.gyrex.context.tests.internal.AllContextTests;
import org.eclipse.gyrex.junit.GyrexServerResource;

import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ /* list test classes here */ })
public class AllServerTests {

  @ClassRule
  public static final GyrexServerResource server = new GyrexServerResource();

}