我正在尝试在Jersey测试类中注入HK2 Factory服务提供的对象,但是获得了不满意的依赖项异常。
我有以下工厂服务
public class TestFactory implements Factory<TestObject>{
private final CloseableService closeService;
@Inject
public TestFactory(CloseableService closeService) {
this.closeService = closeService;
}
@Override
public TestObject provide() {
TestObject casualObject = new TestObject();
this.closeService.add(() -> dispose(casualObject));
return casualObject;
}
@Override
public void dispose(TestObject instance) {
instance.destroy();
}
}
泽西岛测试班
public class SampleTestCass extends JerseyTestNg.ContainerPerClassTest
{
//@Inject
private TestObject myTestObject;
private ServiceLocator locator;
@Override
protected Application configure()
{
ResourceConfig resConfig = new ResourceConfig(MyApi.class);
resConfig.register(getBinder());
locator = setupHK2(getBinder());
return resConfig;
}
// setup local hk2
public setupHK2(AbstractBinder binder)
{
ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
ServiceLocator locator = factory.create("test-locator");
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration dc = dcs.createDynamicConfiguration();
locator.inject(binder);
binder.bind(dc);
dc.commit();
return locator;
}
// get the binder
public AbstractBinder getBinder()
{
return new AbstractBinder() {
@Override
protected void configure() {
bindFactory(TestFactory.class, Singleton.class).to(TestObject.class).in(PerLookup.class);
}
}
}
@BeforeClass
public void beforeClass()
{
myTestObject = locator.getService(TestObject.class);
// use myTestObject
}
@AfterClass
public void afterClass()
{
if (locator != null) {
locator.shutdown();
}
}
@Test()
public void someTest()
{
// some test code...
}
}
低于例外
MultiException有3个例外。他们是:
答案 0 :(得分:2)
Flist<-array(NA,dim=c(length(UpathV),max(lengths(UpathV))))
是泽西岛应用程序中的服务。您创建的CloseableService
不与Jersey应用程序绑定。它只是一个独立的定位器。因此,尝试使用此定位器注册ServiceLocator
会导致失败,因为没有TestFactory
。您在CloseableService
注册的那个工作正常。
不确定您究竟要做什么,但如果您想要访问测试中的服务,您可以做的一件事就是将服务绑定为实例,例如
ResourceConfig