views:

557

answers:

1

I've not seen anything in this area I would recommend to a client. If you've used Spring PortletMVC, how did you test it?

It's easy to test under the level of portlet code, and relatively easy to test on the client side through HtmlUnit, Selenium and the like, but I haven't seen anything that is a "Gray Box" test in the spirit of JSFUnit (which looks to me to be the way forward).

  • Apache's Pluto driver could theoretically be used to bootstrap a test harness. Has anyone tried this?
  • Any stub or data provider approaches?
  • Any approach to address two-phase processing concerns?
+3  A: 

I don't know anything about portlets but here it goes.

There's 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.

Some more related info could be found on his Project PortletUnit blog, including 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.

There's also a blog article written by Nils-Helge Garli Hegvik in 2007 titled 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.

Finally, Spring Framework documentation 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.

Here's a related article written by John Ferguson Smart titled 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());  
 ...        
    }
    ...
eed3si9n