Skip to content
druglee edited this page Nov 30, 2013 · 8 revisions

PortletTester in a nutshell

Now PortletTester is released to Maven Central Repository!!

GroupId: com.portletguru
ArtifactId: portlettester

For who don't use Maven to build their projects, PortletTester is dependent on following jar files:

  • portlet-api-2.0.jar
  • servlet-api.jar (2.4 or later)
  • ccpp-1.0.jar
  • commons-lang-2.6.jar

Please note that the dependencies of the files above has not been investigated by the author. So, you may need to download more jar files manually if necessary when not using Maven.

PortletTest is a framework for Portlet unit testing. It's compatible with portlets developed based on JSR168 or JSR286. This page only gives you a general idea of how PortletTester works. For more use cases, please refer to the PortletTester-Sample project. Now let's take a look at how PortletTester helps your unit test.

The most important class in PortletTester is the PortletTester class. You can get the objects you need directly or the corresponding Generators of the objects needed from this class. The test results can also be retrieved here.

The following example shows a basic scenario to use PortletTester.

public class SamplePortletTest {

    private static PortletTester portletTester;
    private SamplePortlet portlet;

    @BeforeClass
    public static void setupClass() {
            portletTester = new PortletTester();
    }

    @Before
    public void setup() throws PortletException {
            portlet = new SamplePortlet();
	
            PortletConfigGenerator configGenerator = portletTester.getPortletConfigGenerator();
            configGenerator.addInitParameter(SamplePortlet.INIT_PARAM_KEY, "true");
            portletTester.initPortlet(portlet, configGenerator.generatePortletConfig());
    }

    /**
     * Test doView() is able to set saved user id as an attribute
     * 
     * @throws PortletException
     * @throws IOException
     */
    @Test
    public void testDoView() throws PortletException, IOException {
	
            String userId = "123";
	
            RenderRequestGenerator requestGenerator = portletTester.getRenderRequestGenerator();
            RenderResponseGenerator responseGenerator = portletTester.getRenderResponseGenerator();
	
            requestGenerator.setParameter(SamplePortlet.PARAM_USER_ID, userId);
	
            RenderRequest request = requestGenerator.generateRequest();
            RenderResponse response = responseGenerator.generateResponse();
	
            portlet.doView(request, response);
	
            assertEquals(userId, request.getAttribute(SamplePortlet.REQUEST_ATTR_SAVED_USER_ID));
    }

    @After
    public void tearDown() {
            portletTester.reset();
    }

    @AfterClass
    public static void tearDownClass() {
            portletTester = null;
    }
}

Let's drill down to this class step by step.

  • First of all, in the setupClass() method, an instance of PortletTester is created and it's re-used through the lifecycle of this test class. In order to clear the status after a test, we need to reset it. That's why you see "portletTester.reset()" in the tearDown() method.
  • One step forward, you need to initialize the Portlet being tested. If a PortletConfig is needed, the corresponding generator should retrieved. And that's where PortletTester#getPortletConfigGenerator() comes into the picture. You are able to fill with content needed by your PortletConfig object with this generator. For example, adding init-parameter, specifying the publishing events,specifying the processing events, etc. After everything is ready, simply invoke PortletConfigGenerator#generatePortletConfig() to get your PortletConfig instance.
  • In the testDoView() method, similar behavior is applied to RenderRequest and RenderResponse. Once you get your objects ready, just simply run the method you want to test.

Enjoy unit testing!!

Clone this wiki locally