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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值