泽西测试+ HK2 +注入工厂服务

时间:2018-04-30 00:56:30

标签: java jersey-2.0 hk2 jersey-test-framework

我正在尝试在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个例外。他们是:

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

1 个答案:

答案 0 :(得分:2)

Flist<-array(NA,dim=c(length(UpathV),max(lengths(UpathV))))是泽西岛应用程序中的服务。您创建的CloseableService 与Jersey应用程序绑定。它只是一个独立的定位器。因此,尝试使用此定位器注册ServiceLocator会导致失败,因为没有TestFactory。您在CloseableService注册的那个工作正常。

不确定您究竟要做什么,但如果您想要访问测试中的服务,您可以做的一件事就是将服务绑定为实例,例如

ResourceConfig