【问题标题】:Jersey Test + HK2 + Inject Factory serviceJersey Test + HK2 + Inject Factory 服务
【发布时间】:2018-04-30 00:56:30
【问题描述】:

我试图在 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 没有可注入的对象(requiredType=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 上解析

【问题讨论】:

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


    【解决方案1】:

    CloseableService 是一项 Jersey 应用程序中可用的服务。您创建的ServiceLocator绑定到 Jersey 应用程序。它只是一个独立的定位器。所以试图用这个定位器注册TestFactory会导致它失败,因为没有CloseableService。您使用ResourceConfig 注册的那个可以正常工作。

    不确定你到底想做什么,但如果你想在测试中访问服务,你可以做的一件事就是将服务绑定为一个实例,比如

    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);
                    }
                })
        }
    }
    

    【讨论】:

    • 正如您所说,CloseableService(间接)在测试类中使用时会产生问题。它按照您描述的方式工作。我只是想使用非测试类中使用的Factory 服务。
    • 不确定你的意思。如果您说您希望能够在非球衣测试中使用 TestFactory,则模拟出 CloseableService 并绑定模拟。
    • 我的意思是我们 @Inject 在泽西资源类 (@Path) 中的方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多