Announcement Announcement Module
Collapse
No announcement yet.
StrutsTest can't test ActionSupport action? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • StrutsTest can't test ActionSupport action?

    Hi friends:

    I want to test my Struts Action using StrutsTest.I found that if my struts action extends Action,the strutsTest works well.But if struts action extends ActionSupport,it can't work and throws exception!
    Code:
    servletunit.struts.ExceptionDuringTestError: An uncaught exception was thrown during actionExecute()
    	at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:409)
    	at test.TestLoginAction.testLogon(TestLoginAction.java:28)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:324)
    	at junit.framework.TestCase.runTest(TestCase.java:154)
    	at junit.framework.TestCase.runBare(TestCase.java:127)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    ------------
    Root Cause:
    ------------
    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    	at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:83)
    	at org.springframework.web.struts.ActionSupport.initWebApplicationContext(ActionSupport.java:102)
    	at org.springframework.web.struts.ActionSupport.setServlet(ActionSupport.java:78)
    	at org.apache.struts.action.RequestProcessor.processActionCreate(RequestProcessor.java:295)
    	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:218)
    	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    	at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:394)
    	at test.TestLoginAction.testLogon(TestLoginAction.java:28)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:324)
    	at junit.framework.TestCase.runTest(TestCase.java:154)
    	at junit.framework.TestCase.runBare(TestCase.java:127)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    But I registe the ContextLoaderListener in web.xml!
    My web.xml is :
    Code:
    <web-app>
      <display-name>Apache-Axis</display-name>
    
        <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
          /WEB-INF/springmodal-servlet.xml
        </param-value> 
      </context-param> 
    
    <listener> 
                            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
                    </listener> 
    				 ..............................................................................
    My action code is:
    Code:
    public class LogonAction extends  ActionSupport {
    
    	private static final Log log = LogFactory.getLog(LogonAction.class);
    
    	public ActionForward execute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response)
    			throws Exception {
    
    		// Cast form to DynaBean
    		DynaBean dynaForm = (DynaBean) form;
    
    		// Use the DynaBean
    		String username = (String) dynaForm.get("username"); // simple
    		String password = (String) dynaForm.get("password"); // mapped
    		ApplicationContext ac=this.getWebApplicationContext();
    		UserDAO userDao=(UserDAO)ac.getBean("userDao");
    		Userinfo uinfo=userDao.login(username, password);
    ..........................................................................
    It couldn't be test by strutsTest.The following code could be test by strutstest:
    Code:
    public class LogonAction extends   Action  {
    
    	private static final Log log = LogFactory.getLog(LogonAction.class);
    
    	public ActionForward execute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response)
    			throws Exception {
    
    		// Cast form to DynaBean
    		DynaBean dynaForm = (DynaBean) form;
    
    		// Use the DynaBean
    		String username = (String) dynaForm.get("username"); // simple
    		String password = (String) dynaForm.get("password"); // mapped
    		// List products = (List)dynaForm.get("products"); // indexed
    		InputStream is = new FileInputStream("beans.xml");
    XmlBeanFactory factory = new XmlBeanFactory(is);
    		UserDAO userDao=(UserDAO)factory .getBean("userDao");
    		Userinfo uinfo=userDao.login(username, password);
    ...............................................................................
    why? Why struts couldn't test spring's ActionSupport class ? There are many struts action extends ActionSupport other than Action,how to Test them? Help!
    Last edited by lyo; Jan 19th, 2006, 11:29 AM.
Working...
X