Skip to content
Bryan Hunt edited this page Dec 30, 2013 · 7 revisions

ServiceLocator

When building JUnit integration tests in an OSGi environment, you may need to obtain a reference to a service. The ServiceLocator is a JUnit @Rule for just that purpose. The service locator will wait for the service (up to 1 second by default) and fail the test if the service is not found. You use the rule in your test as follows:

public SystemTest
{
  @Rule
  public ServiceLocator<LogService> logServiceLocator = new ServiceLocator<LogService>(LogService.class);

  private LogService logService;
  
  @Before
  public void setUp()
  {
    logService = logServiceLocator.getService();
  }
}

ServiceConfigurator

Some OSGi services require configuration properties. The ServiceConfigurator is an extension of the JUnit ServiceLocator @Rule for supplying those properties. You use the rule in your test as follows:

private static Hashtable<String, Object> serviceProperties = new Hashtable<String, Object>

static
{
  serviceProperties.put("key", "value");
}

@Rule
public ServiceLocator<UserService> userServiceLocator = new ServiceConfigurator<UserService>(UserService.class, "org.example.userService", serviceProperties);

private UserService userService;

public SystemTest
{
  @Before
  public void setUp()
  {
    userService = userServiceLocator.getService();
  }
}
Clone this wiki locally