This project has been submited and merged into HK2.
- https://java.net/jira/browse/HK2-135
- https://java.net/projects/hk2/sources/git/show/hk2/hk2-testing/hk2-testng
Note that the HK2 project took this code as is with minor renaming of packages. The instructions bellow still apply and all you have to do to use HK2 in your tests is simply add the HK2 dependency to your project and imprt the @HK2 annotation:
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-testng</artifactId>
<version>2.2.0-b23</version>
<scope>test</scope>
</dependency>
This project adds HK2 Dependency Injection support to TestNG. It provides the ability to inject your test classes with HK2 services defined in inhibitent files and/or custom binders.
By default the @HK2 annotation creates a new service locator with the name "hk2-testng-locator" and populates it with services defined in "META-INF/hk2-locator/default" classpath inhabitant files. Simply annotate your test class with @HK2 like this to inject discovered services:
@HK2
public class PrimaryInjectionTest {
@Inject
PrimaryService primaryService;
@Test
public void assertPrimaryServiceInjecton() {
assertThat(primaryService).isNotNull();
}
@Test
public void assertSecondaryService() {
assertThat(primaryService.getSecondaryService()).isNotNull();
}
}
If service discovery and population is not desirable then you can turn it off by setting the populate parameter to false and defining your own Binder class(es):
@HK2(populate = false, binders = {CombinedBinder.class})
public class BinderInjectionTest {
@Inject
PrimaryService primaryService;
@Test
public void assertPrimaryServiceInjecton() {
assertThat(primaryService).isNotNull();
}
@Test
public void assertSecondaryService() {
assertThat(primaryService.getSecondaryService()).isNotNull();
}
}
You can also use both service population and your own binder class(es). Simply insure that populate flag is set to true (by default set to true) and specify your binders like this:
@HK2(binders = {PrimaryBinder.class, SecondaryBinder.class})
public class MultipleBinderInjectionTest {
@Inject
PrimaryService primaryService;
@Test
public void assertPrimaryServiceInjecton() {
assertThat(primaryService).isNotNull();
}
@Test
public void assertSecondaryService() {
assertThat(primaryService.getSecondaryService()).isNotNull();
}
}
The above will create a single service locator instance named "hk2-testng-locator" that contains all the discovered services as well as services defined in your binder.
Finally if you wish to use a custom service locator name you can by specifying @HK2 annotation's value parameter:
@HK2("custom")
public class CustomLocatorNameTest {
@Inject
ServiceLocator sericeLocator;
@Inject
PrimaryService primaryService;
@Test
public void assertPrimaryServiceInjecton() {
assertThat(primaryService).isNotNull();
}
@Test
public void assertSecondaryService() {
assertThat(primaryService.getSecondaryService()).isNotNull();
}
@Test
public void assertServiceLocatorIsCustom() {
assertThat(sericeLocator.getName())
.isEqualTo("custom");
}
}
Note that if two test classes are annotated with @HK2("custom") then only one service locator will be created and the tests will share this service locator.
If you wish to use an isolated service locators per test or for certain tests then you will need to define a unique service locator name for these test classes:
@HK2("serviceLocatorNameA")
public class Isolated1Test {
...
}
@HK2("serviceLocatorNameB")
public class Isolated2Test {
....
}