Thursday, 29 March 2012

JUnit Test cases for Struts2 Action class With Spring and hibernate integration


Base Test Class
  1. /**
  2.  *
  3.  */
  4. package com.lkr.base.test;
  5. import java.util.HashMap;
  6. import junit.framework.TestCase;
  7. import org.apache.struts2.ServletActionContext;
  8. import org.apache.struts2.dispatcher.Dispatcher;
  9. import org.apache.struts2.views.JspSupportServlet;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.mock.web.MockHttpServletRequest;
  12. import org.springframework.mock.web.MockHttpServletResponse;
  13. import org.springframework.mock.web.MockServletConfig;
  14. import org.springframework.mock.web.MockServletContext;
  15. import org.springframework.web.context.ContextLoader;
  16. import com.opensymphony.xwork2.ActionProxy;
  17. import com.opensymphony.xwork2.ActionProxyFactory;
  18. import com.opensymphony.xwork2.ActionSupport;
  19. /**
  20.  *
  21.  * @author KONDA REDDY. LINGAMDINNE
  22.  */
  23. public abstract class IDEActionBaseTest extends TestCase {
  24.     private static final String CONFIG_LOCATIONS ="applicationContext.xml"+",ideazExchangeTiles-defs.xml";
  25.     private static ApplicationContext applicationContext;
  26.     private Dispatcher dispatcher;
  27.     protected ActionProxy proxy;
  28.     protected static MockServletContext servletContext;
  29.     protected static MockServletConfig servletConfig;
  30.     protected MockHttpServletRequest request;
  31.     protected MockHttpServletResponse response;
  32.     public IDEActionBaseTest(String name) {
  33.         super(name);
  34.     }
  35.     @SuppressWarnings({ "unchecked", "deprecation", "rawtypes" })
  36.     protected ActionSupport createAction( String namespace, String name)
  37.     throws Exception {
  38.         proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, null, true, false);
  39.         proxy.getInvocation().getInvocationContext().
  40.         setParameters(new HashMap());
  41.         proxy.setExecuteResult(true);
  42.         ServletActionContext.setContext(
  43.         proxy.getInvocation().getInvocationContext());
  44.         request = new MockHttpServletRequest();
  45.         response = new MockHttpServletResponse();
  46.         ServletActionContext.setRequest(request);
  47.         ServletActionContext.setResponse(response);
  48.         ServletActionContext.setServletContext(servletContext);
  49.         return (ActionSupport) proxy.getAction();
  50.     }
  51.     @SuppressWarnings({ "rawtypes", "unchecked" })
  52.     protected void setUp() throws Exception {
  53.         if (applicationContext == null) {
  54.             servletContext = new MockServletContext();
  55.             servletContext.addInitParameter(
  56.                     ContextLoader.CONFIG_LOCATION_PARAM,
  57.                     CONFIG_LOCATIONS);
  58.             applicationContext = (new ContextLoader())
  59.                     .initWebApplicationContext(servletContext);
  60.             new JspSupportServlet().init(new MockServletConfig(servletContext));
  61.         }
  62.         HashMap params = new HashMap();
  63.         params.put("actionPackages", "com.lkr.admin.action");
  64.         dispatcher = new Dispatcher(servletContext, params);
  65.         dispatcher.init();
  66.         Dispatcher.setInstance(dispatcher);
  67.     }
  68.    
  69. }


  1. package com.lkr.base.test;
  2. import com.lkr.view.UsersView;
  3. import com.lkr.admin.action.LoginAction;
  4. public class LoginActionTest extends IDEActionBaseTest {
  5.     public LoginActionTest(String name) {
  6.         super(name);
  7.         // TODO Auto-generated constructor stub
  8.     }
  9.     protected void setUp() throws Exception {
  10.         super.setUp();
  11.     }
  12.     protected void tearDown() throws Exception {
  13.         super.tearDown();
  14.     }
  15.     public void testLogin() throws Exception {
  16.        
  17.         LoginAction loginAction = (LoginAction) createAction("/", "login");
  18.        
  19.         UsersView usersView = new UsersView();
  20.        
  21.         loginAction.setUsersView(usersView);
  22.        
  23.         usersView.setLoginId("use");
  24.         usersView.setPassword("fuhdsiah");
  25.         // loginAction.login();
  26.     //  System.out.println(loginAction.login());
  27.     }
  28. }

No comments:

Post a Comment