我想用简单的JUnit Test(没有Arquillian,没有MicroShed)来测试我的Jakarta应用程序(Payara)。
我使用Hk2中的ServiceLocator为我的JPA实体和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));
}
但是我遇到了一些问题,因为我在FishService中加入了CDI事件。
@Inject
private Event<StockEvent> stockEvent;
然后我触发一个事件。
当我启动测试serviceLocator时,无法创建FishService,并且出现以下错误:
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
在此测试课程中,我不想测试事件CDI功能。
我如何使用Hk2注入CDI事件服务?(我的代码在此github account中托管)
您可以使用Mockito模拟Event
,然后将模拟与AbstractBinder
绑定,并将其添加到ServiceLocator
。这是一个完整的测试
import javax.inject.Inject;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.Binder;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class Hk2CdiInjectTest {
public interface Event<T> {
T get();
}
public interface StockEvent {
String getStock();
}
public static class Service {
@Inject
private Event<StockEvent> event;
public String getStock() {
return event.get().getStock();
}
}
private static ServiceLocator locator;
@BeforeClass
public static void setUpBeforeClass() {
locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
ServiceLocatorUtilities.addClasses(locator, Service.class);
final Event<StockEvent> event = (Event<StockEvent>) mock(Event.class);
final StockEvent stockEvent = mock(StockEvent.class);
when(stockEvent.getStock()).thenReturn("GOOGL UP!");
when(event.get()).thenReturn(stockEvent);
Binder eventBinder = new AbstractBinder() {
@Override
public void configure() {
bind(event).to(new TypeLiteral<Event<StockEvent>>() {});
}
};
ServiceLocatorUtilities.bind(locator, eventBinder);
}
@Test
public void testIt() {
Service service = locator.getService(Service.class);
assertThat(service.getStock()).isEqualTo("GOOGL UP!");
}
}