Skip to content

Instantly share code, notes, and snippets.

@njbartlett
Created December 1, 2011 10:43
Show Gist options
  • Save njbartlett/1415736 to your computer and use it in GitHub Desktop.
Save njbartlett/1415736 to your computer and use it in GitHub Desktop.
Java 7 OSGi testing ideas
public class ExampleBundleTest extends TestCase {
private final BundleContext context = FrameworkUtil.getBundle(ExampleBundleTest.class).getBundleContext();
public void testServiceAvailable() {
// Tests that a specific service exists and can be invoked successfully.
// The service reference is released automatically.
try (ServiceConsumer<MyService> svc = new ServiceConsumer<>(MyService.class, context)) {
assertEquals("Hello Neil", svc.get().sayHello("Neil"));
}
}
public void testServiceInvoked() {
// Temporarily register a service, expecting that somebody invokes it immediately.
// The service is automatically unregistered after the test.
LogService log = mock(LogService.class);
try (ServicePublisher<LogService> sp = new ServicePublisher<>(LogService.class, log, context)) {
verify(log).log(LogService.LOG_INFO, "Expected log message");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment