首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

引见大家一个好的测试Spring Controller的框架

2012-10-26 
介绍大家一个好的测试Spring Controller的框架可能大家都知道了。知道的就掠过吧。其实我想说的就是Spring原

介绍大家一个好的测试Spring Controller的框架
可能大家都知道了。知道的就掠过吧。
其实我想说的就是Spring原厂的Test, 文档可以参见这里:
http://static.springframework.org/spring/docs/2.5.x/reference/testing.html

我给大家一个我写的例子吧。里面有如何mock request/response, 如何往servlet context里面放从xml里面读出来的bean. 例子里面的两个文件就是Spring的bean文件。

这个框架比instinct啊, Jmock啊用起来好不少呢。测试spring+struts应该也同理。

public class SecuredControllerTest extends TestCase {    private HttpServletRequest request = new MockHttpServletRequest();    private HttpServletResponse response = new MockHttpServletResponse();    private XmlWebApplicationContext context;    private MockServletContext msc;    private SecuredController securedController;    protected void setUp() throws Exception {        String[] contexts = new String[] { "tempo-ui-fw-servlet.xml", "tempo-ui-fw.xml" };        context = new XmlWebApplicationContext();        context.setConfigLocations(contexts);        msc = new MockServletContext();        context.setServletContext(msc);        context.refresh();        msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);    }    @Test    public void testShowFormWithoutUser() throws Exception {        securedController = (SecuredController) context.getBean("tasksController");        ModelAndView mav = securedController.showForm(request, response, null);        Assert.assertTrue(mav.getView().toString().contains(Constants.LOGIN_URL));    }    @Test    public void testShowFormWithUser() throws Exception {        FakeUIFWApplicationState state = new FakeUIFWApplicationState();        User currentUser = new User("test1", new String[] { "test/test1" }, "token1");        state.setCurrentUser(currentUser);        request.getSession().setAttribute("APPLICATION_STATE", state);        securedController = (SecuredController) context.getBean("tasksController");        ModelAndView mav = securedController.showForm(request, response, new BindException(currentUser, "test"));        Assert.assertEquals(mav.getViewName(), "tasks");    }    @Test    public void testProcessFormSubmissionWithoutUser() throws Exception {        securedController = (SecuredController) context.getBean("tasksController");        ModelAndView mav = securedController.processFormSubmission(request, response, null, null);        Assert.assertTrue(mav.getView().toString().contains(Constants.LOGIN_URL));    }    @Test    public void testProcessFormSubmissionWithUser() throws Exception {        FakeUIFWApplicationState state = new FakeUIFWApplicationState();        User currentUser = new User("test1", new String[] { "test/test1" }, "token1");        state.setCurrentUser(currentUser);        request.getSession().setAttribute("APPLICATION_STATE", state);        securedController = (SecuredController) context.getBean("tasksController");        ModelAndView mav = securedController.processFormSubmission(request, response, null, new BindException(currentUser, "test"));        Assert.assertEquals(mav.getViewName(), "tasks");    }    @Test    public void testProcessFormSubmissionInvalidActionWithUser() throws Exception {        MockHttpServletRequest mockRequest = new MockHttpServletRequest();        mockRequest.addParameter("actionName", "getTaskList");        request = mockRequest;        FakeUIFWApplicationState state = new FakeUIFWApplicationState();        User currentUser = new User("test1", new String[] { "test/test1" }, "token1");        state.setCurrentUser(currentUser);        request.getSession().setAttribute("APPLICATION_STATE", state);        securedController = (SecuredController) context.getBean("tasksController");        try {            ModelAndView mav = securedController.processFormSubmission(request, response, null, new BindException(currentUser, "test"));            Assert.fail("Invalid Action exception expected");        } catch (Exception e) {        }    }    @Test    public void testProcessFormSubmissionValidActionWithUser() throws Exception {        MockHttpServletRequest mockRequest = new MockHttpServletRequest();        mockRequest.addParameter("actionName", "default");        request = mockRequest;        FakeUIFWApplicationState state = new FakeUIFWApplicationState();        User currentUser = new User("test1", new String[] { "test/test1" }, "token1");        state.setCurrentUser(currentUser);        request.getSession().setAttribute("APPLICATION_STATE", state);        securedController = (SecuredController) context.getBean("tasksController");        ModelAndView mav = securedController.processFormSubmission(request, response, null, new BindException(currentUser, "test"));        Assert.assertEquals(mav.getViewName(), "tasks");    }        @Test    public void testGetCurrentUser() throws Exception {        FakeUIFWApplicationState state = new FakeUIFWApplicationState();        User currentUser = new User("test1", new String[] { "test/test1" }, "token1");        state.setCurrentUser(currentUser);        request.getSession().setAttribute("APPLICATION_STATE", state);        securedController = (SecuredController) context.getBean("tasksController");        String userName = securedController.getCurrentUserName(request);        Assert.assertEquals("test1", userName);    }}


热点排行