gpt4 book ai didi

java - 泽西测试+HK2+注入(inject)工厂服务

转载 作者:行者123 更新时间:2023-12-01 19:54:22 24 4
gpt4 key购买 nike

我试图在 Jersey 测试类中注入(inject) 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();
}
}

还有 Jersey 测试类

    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 个异常。他们是:

  1. org.glassfish.hk2.api.UnsatisfiedDependencyException: SystemInjecteeImpl 没有可用于注入(inject)的对象(requiredType=CloseableService,parent=TestFactory,qualifiers={},position=0,optional=false,self=false,unqualified= null,2053349061)
  2. java.lang.IllegalArgumentException:在尝试解析 com.test.factories.TestFactory 的依赖项时发现错误
  3. java.lang.IllegalStateException:无法执行操作:在 com.test.factories.TestFactory 上解析

最佳答案

CloseableService 是 Jersey 应用程序中可用的服务。您创建的 ServiceLocator 绑定(bind)到 Jersey 应用程序。它只是一个独立的定位器。因此,尝试使用此定位器注册 TestFactory 将导致其失败,因为没有 CloseableService。您在 ResourceConfig 中注册的配置就可以正常工作。

不确定您到底想做什么,但如果您想访问测试中的服务,您可以做的一件事就是将该服务绑定(bind)为实例,例如

class MyTest {
private Service service;

@Override
public ResourceConfig configure() {
service = new Service();
return new ResourceConfig()
.register(new AbstractBinder() {
@Override
public void configure() {
bind(service).to(Service.class);
}
})
}
}

关于java - 泽西测试+HK2+注入(inject)工厂服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50092756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com