技术开发 频道

浅析Mockrunner测试框架

【IT168 技术文章】

  Mockrunner用在J2EE环境中进行应用程序的单元测试。它不仅支持Struts actions,servlets,过滤器和标签类还包括一个JDBC和一个JMS测试框架,可以用于测试基于EJB的应用程序。

  下面提供一个测试Struts应用的例子。

public class OrderAction extends Action
{
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
                                 throws Exception
    {
        OrderForm orderForm = (OrderForm)form;
        String id = orderForm.getId();
        int amount = orderForm.getAmount();
        OrderManager orderManager =
        OrderManager.instance(request.getSession().getServletContext());
        if(orderManager.getStock(id)  amount)
        {
            ActionMessages errors = new ActionMessages();
            ActionMessage error = new ActionMessage(not.enough.in.stock, id);
            errors.add(ActionMessages.GLOBAL_MESSAGE, error);
            saveErrors(request, errors);
            return mapping.findForward(failure);
        }
        orderManager.order(id, amount);
        return mapping.findForward(success);
    }
}

 
  下面假设我们需要mock一个orderManager的实现。

public class OrderActionTest extends BasicActionTestCaseAdapter
{
    private MockOrderManager orderManager;
    private OrderForm form;

    protected void setUp() throws Exception
    {
        super.setUp();
        orderManager = new MockOrderManager();
        ServletContext context = getActionMockObjectFactory().
                                                 getMockServletContext();
        context.setAttribute(OrderManager.class.getName(), orderManager);
        form = (OrderForm)createActionForm(OrderForm.class);
        setValidate(true);
    }

    public void testSuccessfulOrder()
    {
        form.setId(testProduct);
        form.setAmount(10);
        orderManager.setStock(testProduct, 20);
        actionPerform(OrderAction.class, form);
        verifyNoActionErrors();
        verifyNoActionMessages();
        verifyForward(success);
    }
}


  相对于JUnits的测试用例,只需要扩展 BasicActionTestCaseAdapter 或者 ActionTestCaseAdapter。 方法名是自明的,不需加以解释。

public class OrderActionTest extends MyTestCase
{
    private ActionMockObjectFactory mockFactory;
    private ActionTestModule module;
    private MockOrderManager orderManager;
    private OrderForm form;

    protected void setUp() throws Exception
    {
        super.setUp();
        orderManager = new MockOrderManager();
        mockFactory = new ActionMockObjectFactory();
        module = new ActionTestModule(mockFactory);
        ServletContext context = mockFactory.getMockServletContext();
        context.setAttribute(OrderManager.class.getName(), orderManager);
        form = (OrderForm)module.createActionForm(OrderForm.class);
        module.setValidate(true);
    }

    public void testFailureOrder()
    {
        module.addRequestParameter(id, testProduct);
        module.addRequestParameter(amount, 10);
        orderManager.setStock(testProduct, 5);
        module.actionPerform(OrderAction.class, form);
        module.verifyNumberActionErrors(1);
        module.verifyActionErrorPresent(not.enough.in.stock);
        module.verifyActionErrorValue(not.enough.in.stock, testProduct);
        module.verifyNoActionMessages();
        module.verifyForward(failure);
    }
}

    
  It's nearly the same but you can extend your test from your base class MyTestCase. You have to create the ActionMockObjectFactory and the ActionTestModule on your own. In this test we added the parameters to the request instead of setting them directly. The framework does the necessary populating. You can use this mechanism just like in the real Struts environment or you can turn it off for special tests.

0
相关文章