可能大家都知道了。知道的就掠过吧。
其实我想说的就是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);
}
}
分享到:
相关推荐
本例程主要讲spring MVC的框架进行了实现,只需将例程导入myEclipse,上载至apache就可运行,可以看出dispatcherServlet和Controller的关系。
包含基本步骤实现完整的JAVA框架搭建 1 创建web项目,创建dao包,service包,pojo包,controller包,mapper包, 2 导入架包,将架包导入到项目的lib 文件中...现在建立一张表,随便写一个功能,测试下项目是否搭建成功
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
springMVC简单框架
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
Spring是一个开源的Java应用框架,它为企业级Java应用提供了全面的基础设施支持。Spring框架的核心特点包括依赖注入(Dependency Injection)、面向切面编程(Aspect-Oriented Programming)、事务管理、模型-视图-...
Spring Boot 框架 集成 Knife4(源代码) ...零、创建一个SpringBoot项目 一、pom.xml导入Knife4j的依赖 二、创建Knife4j的配置类 三、application.yml配置文件 四、Controller层 五、运行启动类,访问并测试
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...
1. Spring框架:Spring是一个轻量级的Java开发框架,提供了丰富的功能和模块,用于开发企业级应用。它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以...