Test driven development plays a key role in quality assurance. Liferay’s tooling
and integration with standard test frameworks support test driven development
and help you reach quality milestones. You can use Liferay DXP’s @Inject
annotation to inject service components into an integration test, like you use
the @Reference
annotation to inject service components
into an OSGi component.
Follow these steps to inject a service component into a test class:
-
In your test class, add a rule field of type
com.liferay.portal.test.rule.LiferayIntegrationTestRule
. For example,@ClassRule @Rule public static final AggregateTestRule aggregateTestRule = new LiferayIntegrationTestRule();
-
Add a field to hold a service component. Making the field static improves efficiency because the container injects static fields once before test runs and nulls them after all tests run. Non-static fields are injected before each test run but stay in memory till all tests finish.
-
Annotate the field with an
@Inject
annotation. By default, the container injects the field with a service component object matching the field’s type.@Inject
uses reflection to inject a field with a service component object matching the field’s interface. Test ruleLiferayIntegrationTestRule
provides the annotation. -
Optionally add a
filter
string ortype
parameter to further specify the service component object to inject. They can be used separately or together.To fill a field with a particular implementation or sub-class object, set the
type
with it.@Inject(type = SubClass.class)
Replace
SubClass
with the name of the service interface to inject.
At runtime, the @Inject
annotation blocks the test until a matching service
component is available. The block has a timeout and messages are logged
regarding the test’s unavailable dependencies.
Here’s an example test class that injects a DDLServiceUpgrade
object into an
UpgradeStepRegistrator
interface field:
public class Test {
@ClassRule
@Rule
public static final AggregateTestRule aggregateTestRule =
new LiferayIntegrationTestRule();
@Test
public void testSomething() {
// your test code here
}
@Inject(
filter = "(&(objectClass=com.liferay.dynamic.data.lists.internal.upgrade.DDLServiceUpgrade))"
)
private static UpgradeStepRegistrator _upgradeStepRegistrator;
}
Great! Now you can inject service components into your tests.