【问题标题】:Unit testing JAX-RS/Jersey servlet with Guice Injections使用 Guice 注入对 JAX-RS/Jersey servlet 进行单元测试
【发布时间】:2018-08-18 23:44:35
【问题描述】:

我有一个应用程序将 Jersey/JAX-RS 用于 Web 服务(注释等),并使用 Guice 来注入服务实现。我不太喜欢 Guice 直接使用 servlet 的方式,我更喜欢 Jersey 方式,所以我不得不做一些小题大做才能让服务注入工作,因为 Guice 不会创建我的 servlet 类,而我没有不想处理HK2-Guice桥。我通过创建一个侦听器类(称为配置)在应用程序启动时在静态字段中设置注入器来做到这一点,然后通过创建一个父类来手动影响每个 servlet 类中的注入,我的所有 servlet 使用包含以下内容的构造函数扩展该父类:

public MasterServlet() {
    // in order for the Guice @Inject annotation to work, we have to create a constructor
    // like this and call injectMembers(this) on all our injectors in it
    Configuration.getMyServiceInjector().injectMembers(this);
    Configuration.getDriverInjector().injectMembers(this);
}

我知道这有点 hacky,但这在我的 servlet 中工作得很好。我可以在我的服务上使用 Guice @Inject 注释并在命名实现之间切换等等。当我去设置我的单元测试时,问题就来了。我正在使用 JerseyTest 进行测试,但是针对我的 servlet 运行测试会导致 Guice 出现 500 错误,如下所示:

com.google.inject.ConfigurationException:Guice 配置错误: 1) 没有绑定 com.mycompany.MyService 的实现。 同时定位 com.mycompany.MyService 对于 com.mycompany.servlet.TestGetServlet.service(TestGetServlet.java:21) 的字段 同时定位 com.mycompany.servlet.TestGetServlet

测试看起来像这样:

public class TestServletTest extends JerseyTest {

    @Test
    public void testServletFunctional() {
        final String response = target("/testget").request().get(String.class);
        assertEquals("get servlet functional", response);
    }

    @Before
    public void setup() {
        Configuration configuration = new Configuration();
        configuration.contextInitialized(null);
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(TestGetServlet.class);
    }
}

您会注意到在设置方法中我手动创建了我的配置类,因为我不能依赖测试容器 (Grizzly) 来创建它(如果没有这两行,我会得到 NullPointerExceptions)。更多信息请参见下文。

这是正在测试的 servlet:

@Path("/testget")
public class TestGetServlet extends MasterServlet {

    @Inject
    MyService service;

    @GET
    @Produces({"text/plain", MediaType.TEXT_PLAIN})
    public String testGet() {
        //service = Configuration.getServiceInjector().getInstance(MyService.class);
        return "get servlet functional";
    }
}

注意到 testGet() 方法中的注释行了吗?如果我这样做并删除上面的 @Inject 注释,一切正常,这表明 Grizzly 没有按照我期望的方式创建我的 servlet。

我认为发生的事情是灰熊不知道 Guice。一切似乎都表明 Grizzly 没有看到 Configuration 类,尽管通过将它放在我的测试的 @Before 方法中,它似乎至少对使用它的类可用(参见: TestGetServlet 类中的注释行)。我只是不知道如何解决它。

【问题讨论】:

    标签: java servlets guice jersey-2.0 jersey-test-framework


    【解决方案1】:

    我仍在尝试解决这个问题,但与此同时我从 Guice 切换到 HK2,这需要做一些工作,但我认为这可能对将来遇到此问题的任何人有所帮助。

    我认为这是一个答案,因为老实说,我试图绕过 Guice-HK2 桥但仍将 Guice 与 Jersey 一起使用可能不是最好的主意。

    从 Guice 切换到 HK2 需要花费一些时间,并且没有提供所有答案的综合指南。例如,依赖关系真的很挑剔。如果您尝试使用 Jersey 2.27,您可能会遇到著名的

    java.lang.IllegalStateException:找不到 InjectionManagerFactory

    错误。由于 HK2 本身,Jersey 2.27 不向后兼容以前的版本。我仍在努力让这一切正常工作,但与此同时,我不得不将我的所有 Jersey 依赖项降级到 2.26-b06 才能让 HK2 正常工作。

    谢天谢地,Jersey 已经实现了一堆 HK2 样板文件,因此您只需正确使用 @Contract、@Service(有关这些内容,请参阅 HK2 文档),然后是两个如下所示的新类:

    public class MyHK2Binder extends AbstractBinder {
        @Override
        protected void configure() {
            // my service here is a singleton, yours might not be, so just omit the call to in()
            // also, the order here is switched from Guice! very subtle!
            bind(MyServiceImpl.class).to(MyService.class).in(Singleton.class);
        }
    }
    

    还有这个:

    public class MyResourceConfig extends ResourceConfig {
    
        public MyResourceConfig() {
            register(new MyHK2Binder());
            packages(true, "com.mycompany");
        }
    }
    

    足够简单,但这仅适用于应用程序本身。测试容器对此一无所知,因此您必须自己在测试类中重做 Binder 和 ResourceConfig,如下所示:

    public class TestServletTest extends JerseyTest {
    
        @Test
        public void testServletFunctional() {
    
            final String response = target("/testget").request().get(String.class);
            assertEquals("get servlet functional", response);
        }
    
        @Before
        public void setup() {
        }
    
        @Override
        protected Application configure() {
            return new TestServletBinder(TestGetServlet.class);
        }
    
        public class TestServletBinder extends ResourceConfig {
            public TestServletBinder(Class registeree) {
                super(registeree);
                register(new MyHK2Binder());
                packages(true, "com.mycompany");
            }
        }
    }
    

    必须这样做实际上很好,因为您可以将 Binder 切换为测试绑定器,在其中您已将服务绑定到模拟服务或其他东西。我在这里还没有这样做,但这很容易做到:将 register() 调用中的 new MyHK2Binder() 替换为执行这样的绑定:

    bind(MyTestServiceImpl.class).to(MyService.class).in(Singleton.class);
    

    然后瞧。非常好。显然,您可以使用命名绑定获得类似的结果,但这非常有效,甚至可能更简单、更清晰。

    希望这可以帮助某人节省我花时间搞定这项工作的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      相关资源
      最近更新 更多