`
arkxu
  • 浏览: 42694 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

介绍大家一个好的测试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);
    }
}



5
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics