I would like to test my Jakarta application (Payara) with simply JUnit Test (without arquillian, without MicroShed).
I use ServiceLocator from Hk2 to inject service and its work well for my JPA entity and Service.
private static ServiceLocator serviceLocator;
@BeforeAll
public static void setUpBeforeClass() {
serviceLocator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
ServiceLocatorUtilities.addClasses(
serviceLocator,
EntityManagerHK2Factory.class,
ProducerUtils.class,
FishService.class,
ShopRepository.class,
Family.class, Fish.class, FishStockKey.class, Shop.class, Stock.class, WaterType.class);
}
@Test
public void it_should_sell_fish() {
//GIVEN
FishService fishService = serviceLocator.getService(FishService.class);
String shopName = "Magic Fish";
String fishName = "Scalaire";
int quantity = 3;
//WHEN
float bill = fishService.sell(shopName, fishName, quantity);
//THEN
assertThat(bill, is(54f));
}
But I have some issues since I had include CDI event inside my FishService.
@Inject
private Event<StockEvent> stockEvent;
And then I fire an event.
When I launch the test serviceLocator does not manage to create the FishService and I get the following error:
A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Event<StockEvent>,parent=FishService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,976949492)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.meynier.jakarta.service.FishService errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.meynier.jakarta.service.FishService
In this test class I don't want to test the event CDI feature.
How can I use Hk2 to inject the CDI event service ? (My code is host in this github account)