unit-testing之是否有可用的符合标准 (168/286) 的 portlet 测试框架 (尤其是与 Spring PortletMVC 一起使用的那个)

xiaohuochai 阅读:35 2024-02-27 23:08:18 评论:0

我在这方面没有看到任何我会推荐给客户的东西。如果您使用过 Spring PortletMVC,您是如何对其进行测试的?

在portlet代码层面下测试比较容易,在客户端通过HtmlUnit、Selenium等比较容易测试,但是我还没有看到什么是JSFUnit精神的“灰盒”测试(在我看来是前进的道路)。

  • Apache 的 Pluto驱动程序理论上可用于引导测试工具。有没有人试过这个?
  • 任何 stub 或数据提供者方法?
  • 任何解决两阶段处理问题的方法?
  • 请您参考如下方法:

    我对 portlet 一无所知,但它就是这样。

    portletUnit .

    portletUnit is a testing framework used to test JSR-168 portlets outside portlet container just as servletUnit is used to test servlets outside a servlet container. The projected is architected to map the functionally of servletUnit onto portlets with servletUnit itself providing the foundation for portletUnit.



    更多相关信息可以在他的 Project PortletUnit blog 上找到。 ,包括 PortletUnit and Spring Portlet: Checking form validation errors .

    When testing with portletUnit, it is not obvious how to check if there were any form errors. Fortunately, using the render listener feature of PortletRunner, there is a simple way to check for validator errors.



    还有一篇由 Nils-Helge Garli Hegvik 于 2007 年撰写的博客文章,标题为 Testing Portlets with Jetty, Pluto and JWebUnit .

    Remembering an excellent article from Johannes Brodwall's blog about integration testing with Jetty and JWebUnit, I wanted to extend his approach to use the embedded jetty-pluto setup I have created. This turned out to be to be quite easy.



    最后,Spring 框架文档 10.2 Unit testing .

    The org.springframework.mock.web.portlet package contains a set of Portlet API mock objects, targeted at usage with Spring's Portlet MVC framework.

    [...] The org.springframework.test.web package contains ModelAndViewAssert, which can be used in combination with any testing framework (e.g., JUnit 4+, TestNG, etc.) for unit tests dealing with Spring MVC ModelAndView objects.

    [...] To test your Spring MVC Controllers, use ModelAndViewAssert combined with MockHttpServletRequest, MockHttpSession, etc. from the org.springframework.mock.web package.



    这是约翰·弗格森·斯马特 (John Ferguson Smart) 撰写的一篇相关文章,标题为
    Unit testing your Spring-MVC applications .

    One of the great things about this framework is how testable it is. In Spring-MVC, any custom validators (for field and form validation) and property editors (for converting text fields to specific Java types) are dead-easy to test - you can just test them as if they where isolated POJOs.

    Spring-MVC also comes with a full set of mock objects that you can use (with a bit of practice) to test your controllers to your heart's content. For example, you can use classes like MockHttpServletRequest and MockHttpServletResponse to simulate your HTTP request and response objects. This is also made easier by the fact that Controllers can be instanciated as normal Java classes. For example, imagine you are testing a controller class for a page that updates a client details record. You could do this very simply as follows:


    public class UpdateClientTest { 
            // 
            // Prepare your request 
            // 
            request.setMethod("POST");       
            request.setParameter("id", "100"); 
            request.setParameter("firstName", "Jane"); 
            request.setParameter("lastName", "Doe"); 
            // 
            // Invoke the controller 
            // 
        controller = new ChoosePeriodController(); 
            ModelAndView mav = controller.handleRequest(request, response); 
        // 
        // Inject any service objects you need 
        // 
            controller.setClientService(clientService); 
        ... 
            // 
            // Inspect the results 
            // 
            assert mav != null; 
            assertEquals("displayClient",mav.getViewName());   
            Client client = (Client) mav.getModel().get("client"); 
            assertEquals("Jane",client.getFirstName());   
            assertEquals("Doe",client.getLastName());   
        ...         
        } 
        ... 
    


    标签:Spring
    声明

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    关注我们

    一个IT知识分享的公众号