...
- Inject service references automatically to your test class
- Configure services and service factories using ConfigurationAdmin
- Define custom components as part of the test setup.
- Automatic configuration of the MongoDBService
Usage
Test configuration should be done in your test's setUp() method by invoking configure(this) followed by one or more ConfigurationSteps. These steps should be created using the static methods provided by amdatu-testing. The apply() method should be called after all steps have been specified. All steps should be cleaned up on tear down by calling cleanUp(this).
The example below demonstrates how to inject a service (SampleService) to our test case, as well as configure a service and service factory.
Code Block | ||
---|---|---|
| ||
import static org.amdatu.testing.configurator.TestConfigurator.cleanUp;
import static org.amdatu.testing.configurator.TestConfigurator.configuration;
import static org.amdatu.testing.configurator.TestConfigurator.configure;
import static org.amdatu.testing.configurator.TestConfigurator.factoryConfiguration;
import static org.amdatu.testing.configurator.TestConfigurator.serviceDependency;
import junit.framework.TestCase;
import org.amdatu.releasetest.api.SampleService;
public class SampleServiceTest extends TestCase {
private volatile SampleService myService;
@Override
public void setUp() throws Exception {
super.setUp();
configure(this)
.add(configuration("my.managed.service") // configure a ManagedService
.set("someProperty", "someValue")
.set("anotherProperty", "anotherValue"))
.add(factoryConfiguration("my.managed.service.factory") // configure a ManagedServiceFactory
.set("someProperty", "someValue"))
.add(serviceDependency(SampleService.class))
.apply(); // inject the dependency
}
@Override
public void tearDown() throws Exception {
super.tearDown();
cleanUp(this); // this performs cleanup automatically for each operation performed in the setUp() method.
}
} |
Specifying a service filter
It is sometimes required to specify a service dependency with a filter. This can be done easily by passing an additional parameter to the serviceDependency() method as follows:
Code Block | ||
---|---|---|
| ||
configure(this)
.add(serviceDependency(MyService.class, "(someKey=someValue)")
.apply(); |
Defining components
It is sometimes required to define a new OSGi component as part of the test setup. Here is an example demonstrating how to create a new component and specify its dependencies:
Code Block | ||
---|---|---|
| ||
configure(this)
.add(component()
.setService(MyService.class)
.setImplementation(MyServiceImpl.class)
.add(serviceDependency(MyDependency.class))
.apply();
} |
Providing groups of reusable configuration steps
It's not uncommon to have multiple tests in your project performing rather similar configurations on setup. For example defining database configurations. amdatu-testing lets you conveniently wrap a bunch of configuration steps and reuse them throughout your project. Below is an example demonstrating a reusable database configuration and its usage.
Code Block | ||
---|---|---|
| ||
// TestUtils.java
class TestUtils {
public static ConfigurationSteps databaseConfiguration() {
return ConfigurationSteps.create()
.add(configure("my.db.service.pid")
.set("someParam", "someValue")
.set("someOtherParam", "someOtherValue");
}
}
// MyTest.java
import static ... // all necessary amdatu-testing static methods
import static TestUtils.databaseConfiguration;
class MyTest extends TestCase {
public void setUp() throws Exception {
configure(this)
.add(databaseConfiguration())
.add(...) // any other test specific configuration steps
.apply();
}
} |
Configuring MongoDBService (org.amdatu.mongo)
When your test needs to access a service that uses MongoDBService, it is required to provide a minimal configuration specifying the DB name to use. Amdatu Testing can do that automatically for you, setting up the MongoDBService to use a temporary database, and performing automatic cleanup. Here is a simple test that performs this configuration and injects the MongoDBService itself:
Code Block | ||
---|---|---|
| ||
import static org.amdatu.testing.configurator.TestConfigurator.cleanUp;
import static org.amdatu.testing.configurator.TestConfigurator.configure;
import static org.amdatu.testing.configurator.TestConfigurator.serviceDependency;
import junit.framework.TestCase;
import org.amdatu.mongo.MongoDBService;
import org.amdatu.testing.mongo.OSGiMongoTestConfigurator;
public class MyTestClass extends TestCase {
private volatile MongoDBService m_mongoDBService;
@Override
public void setUp() throws Exception {
super.setUp();
configure(this)
.add(OSGiMongoTestConfigurator.configureMongoDb()) // configure a MongoDBService
.add(serviceDependency(MongoDBService.class)) // inject the mongoDBService
.apply();
// m_mongoDBService is now available to use within tests
}
@Override
public void tearDown() throws Exception {
super.tearDown();
cleanUp(this);
}
} |