读书人

【转】:Unit Testing Struts 二 Acti

发布时间: 2012-11-10 10:48:50 作者: rapoo

【转】:Unit Testing Struts 2 Actions

?

from:http://glindholm.wordpress.com/2008/06/30/unit-testing-struts-2-actions/

实在没办法,该网站被墙,转载学习

?

Hopefully this will help others who are trying to unit test Struts 2 Actions.

?

My goal is to be able to unit test my actions in the full Struts 2 context with the Interceptor stack being run which includes validation.? The big advantage of this type of testing is that it allows you test you validation logic, the Interceptor configuration for you actions, and the results configuration.

?

The current information on Struts 2 website regarding unit testing was not very helpful.? The guides page has 2 links to external blogs with some example code for unit testing with Spring.? I used these as starting points but since I’m not using Spring and the examples were heavily dependent on Spring I ended up spending a lot of time in the debugger figuring out how to make this work.

?

Below is my StrutsTestContext class it makes use of?Mockrunner?mock Http Servlet classes (mockrunner-servlet.jar).? (If you wish to use a different mock package it should be easy enough to make the change.)

?

The way this works is you first have to create a?Dispatcher?which reads your struts configuration. You then use the Dispatcher to create anActionProxy?with the desired request parameters and session attributes.? The ActionProxy will give you access to the Action object so you can set properties or inject mock objects for your test.? You next execute the ActionProxy to run the Interceptor stack and your action, this returns the result so you can test it for correctness.? You can also test the mock Http servlet objects to ensure other result effects have occured (e.g. a session attribute was changed.)

?

This has been updated for Struts 2.1.6 on 3/5/2009.

?

?

?

?

Here is an example of using this class to unit test a Login Action.

?

/*** Create a Dispatcher.** This is an expensive operation as it has to load all** the struts configuration so you will want to reuse the Dispatcher for** multiple tests instead of re-creating it each time.**** In this example I'm setting configuration parameter to override the** values in struts.xml.*/  HashMap<String, String> params = new HashMap<String, String>();  // Override struts.xml config constants to use a guice test module  params.put("struts.objectFactory", "guice");  params.put("guice.module", "test.MyModule");  MockServletContext servletContext = new MockServletContext();  Dispatcher dispatcher = StrutsTestContext.prepareDispatcher(       servletContext, params);/***  Create an ActionProxy based on the namespace and action.**  Pass in request parameters and session attributes needed for this**  test.*/  StrutsTestContext context = new StrutsTestContext(      dispatcher, servletContext);  Map<String, String> requestParams = new HashMap<String, String>();  Map<String, Object> sessionAttributes = new HashMap<String, Object>();  requestParams.put("username", "test");  requestParams.put("password", "test");  ActionProxy proxy = context.createActionProxy(      "/admin",      // namespace      "LoginSubmit", // Action      requestParams,      sessionAttributes);  assertTrue(proxy.getAction() instanceof LoginAction);  // Get the Action object from the proxy  LoginAction action = (LoginAction) proxy.getAction();  // Inject any mock objects or set any action properties needed  action.setXXX(new MockXXX());  // Run the Struts Interceptor stack and the Action  String result = proxy.execute();  // Check the results  assertEquals("success", result);  // Check the user was redirected as expected  assertEquals(true, context.getMockResponse().wasRedirectSent());  assertEquals("/admin/WelcomeUser.action",      context.getMockResponse().getHeader("Location"));  // Check the session Login object was set  assertEquals(mockLogin,      context.getMockRequest().getSession().getAttribute(         Constants.SESSION_LOGIN));

?

?

?

?

读书人网 >软件架构设计

热点推荐