【问题标题】:How do I pass the HttpServletRequest object to the test case? [duplicate]如何将 HttpServletRequest 对象传递给测试用例? [复制]
【发布时间】:2012-07-30 12:30:24
【问题描述】:

现在我正在编写我班级的测试用例。我想将 HttpServletRequest 对象参数传递给我的测试用例方法以检查该方法是否有效。所以任何人给我的建议。

public void testCheckBatchExecutionSchedule() throws Exception
    {
        assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request));
    }

【问题讨论】:

    标签: java unit-testing junit


    【解决方案1】:

    Spring 提供了一个名为MockHttpServletRequest 的类,可用于测试需要HttpServletRequest 的代码。

    public void testCheckBatchExecutionSchedule() throws Exception
    {
       MockHttpServletRequest request = new MockHttpServletRequest();
       request.addParameter("parameterName", "someValue");
       assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request));
    }
    

    【讨论】:

    • 我在网上找到了这个类的其他模拟,这是唯一一个包含与原始类类似的逻辑的模拟。例如,getRequestURL() 实际上是从其他属性组装一个 URL,而不仅仅是反刍使用 setRequestURL() 或构造函数指定的内容。
    【解决方案2】:

    您应该使用模拟库模拟出请求对象,例如 http://code.google.com/p/mockito/

    public void testCheckBatchExecutionSchedule() throws Exception
    {
       HttpServletRequest mockRequest = mock(HttpServletRequest.class);
       //setup the behaviour here (or do it in setup method or something)
       when(mockRequest.getParameter("parameterName")).thenReturn("someValue");
       assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(mockRequest));
    }
    

    【讨论】:

      【解决方案3】:

      HttpServletRequest 是一个接口。过去,我只是简单地创建了一个类(例如TestHttpServletRequest),它对于HttpServletRequest 中的每个方法都有一个空的方法体,除了我真正需要的那些。对于大多数方法,我返回了一个实例变量并为该实例变量包含了一个 setter,以便测试用例可以定义要返回的内容。 HttpServletRequest有很多方法,但是大多数IDE(我用Eclipse)都可以生成方法存根。

      HttpServletRequestWrapper 的问题在于它仍然需要将另一个HttpServletRequest 传递到其构造函数中,以作为每个方法的默认行为。传递null 会产生NullPointerException

      【讨论】:

        【解决方案4】:

        2018 年 2 月更新:OpenBrace Limited has closed down,不再支持其 ObMimic 产品。

        您也可以使用ObMimic Servlet API 测试替身库:

        import com.openbrace.obmimic.mimic.servlet.http.HttpServletRequestMimic;
        
        public void testCheckBatchExecutionSchedule() throws Exception
        {
           HttpServletRequestMimic request = new HttpServletRequestMimic();
           // Configure the request as necessary...
           // e.g. request.getMimicState().getRequestParameters().set("name", "value");
           assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request));
        }
        

        要配置请求,HttpServletRequestMimic 有一个 getMimicState() 方法,该方法返回一个 HttpServletRequestState,通过该方法可以配置请求的所有相关详细信息(您可以通过该方法访问任何关联的 ServletContext、HttpSession 等,并在必要时进行类似的配置)。 HttpServletRequestState 的文档包括其属性和方法的summary 以及完整详细的Javadoc

        注意:

        • ObMimic 还为 HttpServletResponse、ServletContext、HttpSession、ServletConfig 等提供了类似的“模拟”类。

        • ObMimic 的免费“社区版”可从网站的download 页面获取。

        • 为此,您需要添加到项目中的唯一库是 ObMimic 的 /lib/obmimic.jar(假设 Servlet API 本身已经存在)。

        • ObMimic 网站提供完整的文档,包括 Getting Started 指南、一组带有示例代码的 How To 指南、详细的 Javadoc 等。

        【讨论】:

          【解决方案5】:

          使用tomcat提供的API可以得到HttpServletRequest对象

          HttpServletRequest request = (HttpServletRequest)org.apache.catalina.core.ApplicationFilterChain.getLastServicedRequest();
          

          这将获取传递给 servlet 以从当前线程进行服务的最后一个请求。

          这项工作仅在 Tomcat 的“Strict Servlet Compliance”模式下工作。启用它添加以下JVM参数:

          org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true
          

          【讨论】:

            猜你喜欢
            • 2013-06-15
            • 1970-01-01
            • 1970-01-01
            • 2016-05-11
            • 2018-02-16
            • 2021-09-25
            • 1970-01-01
            • 2023-04-02
            • 1970-01-01
            相关资源
            最近更新 更多