0

我有使用Spring进行依赖注入(spring-jersey模块)和hibernate进行对象关系映射(ORM)的泽西Web服务。为了发展 集成测试,考虑到下列条件:使用Spring进行依赖注入的Jersey web服务的集成测试无法初始化@Context HttpServletRequest

  1. Intiailize测试容器只有一次整个测试类
  2. 注册的监听器,过滤器的servlet等,以测试 容器
  3. 制作确保@Context HttpServletRequest的不是null

根据这一https://java.net/jira/browse/JERSEY-2412泽西项目JIRA任务HttpServletRequest为空,如任务的分辨率所示。作为设计工作。 当灰熊容器中运行集成测试,它运行集成测试上HTTP服务器因此,上基于servlet特征,例如如 HttpServletRequest的,HttpServletResponse的等不可用的任何依赖。

人们似乎对如何解决这一问题没有标准溶液,并泽西社区贡献者的 在这个https://java.net/jira/browse/JERSEY-2417 JIRA票表示正在开发这样的特征明显打开。在此功能实施之前,可能的解决方法是什么?根据我的研究 我来跨越几个职位,状态:

  1. 使用外部容器(如果我没有想太多怎么办?)
  2. 使用球衣的码头模块(如果我不想要什么使用码头?)
  3. ,并不适用于这个项目(因为我们不使用Spring MVC
  4. 用SpringMVC具体的解决方案)

那么,什么是成功运行在位于新泽西州的Web服务器集成测试的最佳解决方案使用弹簧平台桥进行依赖性注入,并依靠 基于Servlet的功能?

回答

0

这是来自新泽西州的标准行为和允许基于servlet的功能,例如HttpServletRequest的还不可用。根据我的研究, 我能达到以下条件

  1. Intiailize测试容器只有一次整个测试类
  2. 注册的监听器,过滤器的servlet等,以测试 容器
  3. 确保@Context HttpServletRequest不为空

通过启动/停止手动灰熊容器并在基于自定义球衣的WebappContext上部署灰熊容器实例。具体步骤按照 情况下,任何人都碰到过这样的问题

  1. 创建的HttpServer
  2. 在@Before创建自定义WebappContext这反映了你的 的web.xml和使用部署HttpServer的实例在WebappContext
  3. 为了确保基于Servlet的功能,如 的Http ServletRequest中可在集成测试使用ServletRegistration加载一个Servlet容器内部应用程序如下图所示

第1步

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) 
public class ResourceEndpointIntegrationTest{ 
    @Context 
    private HttpServletRequest httpReq; 

    private static Logger logger = Logger.getLogger(ResourceEndpointIntegrationTest.class); 

    public static final String BASE_URI = "http://localhost:8989/"; 
    private static HttpServer server = null; 

    @BeforeClass 
    public static void initTest() { 
     RestAssured.baseURI = "http://localhost:8989/"; 
    } 
... 
} 

使用SpringJUnit4ClassRunner.class@ContextConfiguration加载applicationContext.xml进行测试。同时声明@Context HttpServletRequest并创建 HttpServer的一个实例稍后使用。我在此处使用@BeforeClass以用于Rest-Assured的特定目的(您不必使用它),它是可选的。

步骤#2

@Before 
    public void setup() throws Exception { 
     if (server == null) { 
      System.out.println("Initializing an instance of Grizzly Container ..."); 
      final ResourceConfig rc = new ResourceConfig(ResourceEndpointIntegrationTest.class, ..., ..., ...); //update 

      WebappContext ctx = new WebappContext("IntegrationTestContext"); 
         //register your listeners from web.xml in here 
      ctx.addListener("com.xxx.yyy.XEndpointServletContextListener"); 
         //register your applicationContext.xml here 
      ctx.addContextInitParameter("contextConfigLocation", "classpath:applicationContext.xml"); 

         //ServletRegistration is needed to load the ResourceConfig rc inside ServletContainer or you will have no 
         //Servlet-based features available 
      ServletRegistration registration = ctx.addServlet("ServletContainer", 
        new ServletContainer(rc)); 

         //Initialize the Grizzly server passing it base URL 
      server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI)); 

         //Deploy the server using our custom context 
      ctx.deploy(server); 
     } 
    } 

设置@Before运行对每个测试,但,如果条件将迫使设置方法表现得像@BeforeClass允许我们初始化 服务器一次为整个测试类减去静态性质@BeforeClass

我之所以初始化服务器一次测试类中所有的测试是因为,如果我们不那么我们将有

  1. 启动服务器时以下工作流程
  2. 春天的开始扫描豆类
  3. 春季自动布线
  4. 春季布置sessionFactory等
  5. 测试运行
  6. 春天破坏的背景下
  7. 服务器关闭
  8. 重复步骤#1至#7的每个测试

上面是非常耗时,技术上并不可行,因此初始化容器一次(这就是为什么我不扩展它的原因是我想控制测试容器的启动/关闭)。

#步骤3

@AfterClass 
    public static void tearDown() throws Exception { 
     System.out.println("Integration tests completed. Tear down the server ..."); 
     if (server != null && server.isStarted()) { 
      server.shutdownNow(); 
      System.out.println("Grizzly instance shutdown completed"); 
     } 
    } 

在步骤3中,我们使用@AfterClass关机用于集成测试的目的灰熊实例。

进行了抽样检测看起来遵循

@Test 
    public void testCreateSomethingReturnSuccessfully() { 
     JSONObject something = new JSONObject(); 
     cust.put("name", "integrationTest"); 
     cust.put("age", 33); 

     given(). 
      contentType(ContentType.JSON). 
      body(something.toString()).post("/someEndpoint"). 
     then(). 
      statusCode(200). 
     assertThat(). 
      body("id", greaterThan(0)). 
      body("name", equalTo("integrationTest")). 
      body("age", equalTo(33)); 
    } 

(摇篮)一些培训相关的depdendencies

compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.23.2' 
compile group: 'org.glassfish.jersey.test-framework.providers', name:'jersey-test-framework-provider-grizzly2', version:'2.23.2' 
compile group: 'org.springframework', name:'spring-test', version:'4.3.2.RELEASE' 
compile group: 'io.rest-assured', name:'rest-assured', version:'3.0.1' 


// Spring 
    compile group: 'org.springframework', name: 'spring-core', version: '4.3.2.RELEASE' 
    compile group: 'org.springframework', name: 'spring-beans', version: '4.3.2.RELEASE' 
    compile group: 'org.springframework', name: 'spring-web', version: '4.3.2.RELEASE' 
    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.2.RELEASE' 
    compile group: 'org.springframework', name: 'spring-orm', version: '4.3.2.RELEASE' 

    // Jersey-Spring bridge 
    compile (group: 'org.glassfish.jersey.ext', name: 'jersey-spring3', version: '2.23.2'){ 
     exclude group: 'org.springframework', module: 'spring-core' 
     exclude group: 'org.springframework', module: 'spring-web' 
     exclude group: 'org.springframework', module: 'spring-beans' 
     exclude group: 'org.springframework', module: 'spring-jdbc' 
     exclude group: 'org.springframework', module: 'spring-orm' 
    } 

一些进口

import javax.servlet.http.HttpServletRequest; 
import javax.ws.rs.core.Context; 

import org.glassfish.grizzly.http.server.HttpServer; 
import org.glassfish.grizzly.servlet.ServletRegistration; 
import org.glassfish.grizzly.servlet.WebappContext; 
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; 
import org.glassfish.jersey.server.ResourceConfig; 
import org.glassfish.jersey.servlet.ServletContainer; 

import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
相关问题