Unit testing with Stripes

Unit testingI’ll describe here how to do unit  testing on ActionBeans when working with Stripes technology. More specifically, I’ll focus on testing approach using MockRoundtrips.

Stripes comes with large set of mock objects which implement interfaces in Servlet specification. We’ll focus on two of them: MockServletContext and MockRoundtrip. These two can be used to simulate environment in which ActionBeans get executed. It’s good approach to wrap MockServletContext in TestFixture to prevent from MockServletContext getting instantiated for every test case. For large number of test cases that could take a while. Here is the example of test fixture:

public class TestFixture {
    private static MockServletContext context;

    public static MockServletContext getServletContext() {
        MockServletContext ctx = new MockServletContext("test");
        Map filterParams = new HashMap();
        filterParams.put("ActionResolver.Packages", "net.pkg.actions");
        filterParams.put("ActionBeanContext.Class", "net.pkg.controller");
        ctx.addFilter(StripesFilter.class, "StripesFilter", filterParams);
	ctx.addFilter(DynamicMappingFilter.class, "DynamicMappingFilter",
                filterParams);
        ctx.setServlet(DispatcherServlet.class, "StripesDispatcher", null);
        TestFixture.context = ctx;
        return TestFixture.context;
}

While TestFixture is taking care of instantiating MockServletContext, we’ll use MockRoundtrip to write actual tests. Here is a basic example of using MockRoundtrip.

@Test
public void testAddition() throws Exception {

    MockServletContext ctx = TestFixture.getServletContext();
    MockRoundtrip trip = new MockRoundtrip(ctx, AdditionActionBean.class);
    trip.setParameter("firstNumber", "9");
    trip.setParameter("secondNumber", "3");
    trip.execute();

    AdditionActionBean bean = trip.getActionBean(AdditionActionBean.class);
    Assert.assertEquals(bean.getResult(), 12);
    Assert.assertEquals(trip.getDestination(), "/additionResult.jsp");
}

Here is what’s going on. First we get the MockServletContext from TestFixture and use it to instantiate MockRoundtrip.  The context is used to help generate the URL for the request and later to process the request. After the MockRoundtrip is created, we set two string parameters which will be included in the request. Finally, call trip.execute() executes the request in the servlet context. This will execute default operation (annotated  with @DefaultHandler) in our action bean class, AdditionActionBean. There is another execute() method that takes an event name as a parameter. This one can be used to execute the event. In that case the request will be formatted as if the requested event had been submitted and appropriate operation from the action bean will be executed (annotated with @HandlesEvent(EVENT_NAME)).

In the end comes verification. We get ActionBean instance from the MockRoundtrip that has just handled the request and was instantiated by Stripes. First assertion checks that correct result was calculated and second one checks that ActionBean forwarded or redirected user to the correct destination page.

With assertions we conclude with this example. Example shows basic steps of how to do the unit testing with Stripes. Hope it will help.

AdditionActionBean