泽西测试框架和HK2-吉塞桥 [英] Jersey Test Framework & HK2-Guice bridge

查看:119
本文介绍了泽西测试框架和HK2-吉塞桥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在基于泽西岛2.23.2&的REST Web服务. Guice 3.0.要使用Guice,必须调整 hk2-guice桥( m使用2.5.0-b07).一切正常,直到我尝试使用 Jersey测试框架.无法配置hk2-guice桥进行测试.

There are REST web service based on Jersey 2.23.2 & Guice 3.0. To use Guice it is necessary to adjust the hk2-guice bridge (I'm using 2.5.0-b07). Everything works fine until I have tried to test the service using Jersey Test Framework. Can't configure hk2-guice bridge for tests.

我的测试:

public class SomeTest extends JerseyTestNg.ContainerPerClassTest {

    @Override
    protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    protected Application configure() {
        return super.configure(); // cant't configure
    }

    @Test
    public void test() {
        Assert.assertEquals(1, 1);
    }
}

我无法在SomeTest.configure()中配置测试,只是返回new JerseyConfiguration()(请参见下文),因为JerseyConfiguration的构造函数期望ServiceLocator的对象.

I can't configure test in SomeTest.configure() just returning new JerseyConfiguration() (see below) because JerseyConfiguration's constructor expected ServiceLocator's object.

即使有可能返回"JerseyConfiguration"类的对象,我也不确定我的测试是否可行,因为在web.xml文件中定义了一些过滤器和侦听器.

Even if it were possible to return an object of class `JerseyConfiguration` - I'm not sure that my test would work because some filters and listener are defined in the web.xml file.

如何在考虑所有过滤器,侦听器和hk2-guice网桥的情况下配置测试?

Web服务详细信息

Web service details

build.gradle中的依赖项"部分:

Dependencies section from build.gradle:

def jerseyVersion = '2.23.2'
def hk2Version = '2.5.0-b07'
def giuceVersion = '3.0'

dependencies {
    compile "javax.servlet:javax.servlet-api:3.1.0"
    //jersey
    compile "org.glassfish.jersey.core:jersey-server:${jerseyVersion}"
    compile "org.glassfish.jersey.containers:jersey-container-servlet:${jerseyVersion}"
    //hk2
    compile "org.glassfish.hk2:guice-bridge:${hk2Version}"
    //guice
    compile "com.google.inject:guice:${giuceVersion}"
    compile "com.google.inject.extensions:guice-servlet:${giuceVersion}"
}

文件web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>REST API App</display-name>

    <listener>
        <listener-class>com.example.core.JerseyGuiceServletContextListener</listener-class>
    </listener>

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>jerseyFilter</filter-name>
        <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.example.core.JerseyConfiguration</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jerseyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

JerseyGuiceServletContextListener类:

Class JerseyGuiceServletContextListener:

public class JerseyGuiceServletContextListener extends GuiceServletContextListener {
    static Injector injector;

    @Override
    protected Injector getInjector() {
        injector = Guice.createInjector(new JerseyServletModuleConfig());
        return injector;
    }
}

JerseyServletModuleConfig类:

Class JerseyServletModuleConfig:

class JerseyServletModuleConfig extends ServletModule {
    @Override
    protected void configureServlets() {
        bind(HeyResource.class).in(Scopes.SINGLETON);
    }
}

JerseyConfiguration类:

Class JerseyConfiguration:

package com.example.core;

import com.google.inject.Injector;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.server.ResourceConfig;
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;

import javax.inject.Inject;
import javax.servlet.ServletContext;

class JerseyConfiguration extends ResourceConfig {
    @Inject
    public JerseyConfiguration(ServiceLocator serviceLocator, ServletContext servletContext) {
        packages("com.example.ws");
         GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
         GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
         guiceBridge.bridgeGuiceInjector(JerseyGuiceServletContextListener.injector);
    }
}

推荐答案

仅覆盖configureDeployment()&修改了configure()测试中的方法

just overrode configureDeployment() & modified configure() methods in test

public class SomeTest extends JerseyTestNg.ContainerPerClassTest {

    @Override
    protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
        return new GrizzlyWebTestContainerFactory();
    }

@Override
    protected Application configure() {
        return new ResourceConfig()
                .packages("com.example.ws")
                .register(GensonCustomResolver.class);
    }

    @Override
    protected DeploymentContext configureDeployment() {
        Application application = configure();

        return ServletDeploymentContext.builder(application)
                .addListener(JerseyGuiceServletContextListener.class)
                .addFilter(GuiceFilter.class, "guiceFilter")
                .addFilter(ServletContainer.class, "jerseyFilter", new HashMap<String, String>(){{
                    put("javax.ws.rs.Application", JerseyConfiguration.class.getCanonicalName());
                }})
                .build();
    }

    @Test
    public void test() {
        Assert.assertEquals(1, 1);
    }
}

&添加web.xml以测试类路径.

& add web.xml to test classpath.

这篇关于泽西测试框架和HK2-吉塞桥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆