why does StrutsJunit4TestCase invoke the execute() method in a struts rest configured web application?

StackOverflow https://stackoverflow.com/questions/9989852

سؤال

I recently write a web application with Struts2 REST, and I ran the junit test for a Action class then I got an error:

java.lang.NoSuchMethodException: cn.fshk.stock.struts.biz.admin.AuthorizationController.execute()

But it seems to run correctly in Tomcat.

Dose it suppose to invoke the index() method in a Struts2 REST controller?

Can anyone give me some help?

  • here is the struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
        ...        
    <struts>
    
      <constant name="struts.convention.action.suffix" value="Controller" />
      <constant name="struts.convention.action.mapAllMatches" value="true" />
      <constant name="struts.convention.default.parent.package" value="rest-default" />
      <constant name="struts.convention.package.locators" value="rest,struts,struts2,action,actions" />
      <constant name="struts.convention.result.path" value="/" />
      <constant name="struts.convention.action.packages" value="cn.fshk.stock.struts" />
      <constant name="struts.action.extension" value="action,do,,xhtml,xml,json" />
      <constant name="struts.mapper.class" value="org.apache.struts2.rest.RestActionMapper" />
    
    </struts>
    
  • The struts.properties:

    struts.i18n.encoding=UTF-8
    struts.objectFactory = spring
    struts.objectFactory.spring.autoWire = name
    struts.objectFactory.spring.useClassCache = true
    struts.multipart.parser=jakarta
    struts.multipart.saveDir=
    struts.multipart.maxSize=2097152
    struts.serve.static=true
    struts.serve.static.browserCache=true
    struts.tag.altSyntax=true
    struts.devMode = true
    struts.i18n.reload=false
    struts.ui.theme=xhtml
    struts.ui.templateDir=template
    struts.ui.templateSuffix=ftl
    struts.configuration.xml.reload=false
    struts.url.http.port = 80
    struts.url.https.port = 443
    struts.custom.i18n.resources=globalmessages
    struts.dispatcher.parametersWorkaround = false
    struts.freemarker.wrapper.altMap=true
    struts.xslt.nocache=false
    
  • The TestCase Class

    package cn.fshk.stock2.struts;
          ...
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath*:application*.xml" })
    @TransactionConfiguration(transactionManager = "transactionManager")
    public class TestStrutsBase<ActionName> extends StrutsSpringJUnit4TestCase<ActionName> {
    
        Logger logger = Logger.getLogger("cn.fshk");
    
        @Test
        public void testStrutsUtil() throws UnsupportedEncodingException, ServletException {
    
            String result = executeAction("/biz/admin/authorization");            
            logger.debug(result);
         }
    
    }
    
هل كانت مفيدة؟

المحلول

ok, I find out what's wrong.

I need to add

    request.setMethod("GET");

before

    excuteAction("...");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top