0

我最近使用 Struts2 REST 编写了一个 Web 应用程序,并为一个 Action 类运行了 junit 测试,然后出现错误:

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

但它似乎在 Tomcat 中正确运行。

是否假设在 Struts2 REST 控制器中调用 index() 方法?

谁能给我一些帮助?

  • 这是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>
    
  • 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
    
  • 测试用例类

    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);
         }
    
    }
    
4

1 回答 1

0

好的,我找出问题所在。

我需要添加

    request.setMethod("GET");

    excuteAction("...");
于 2012-04-04T03:22:00.907 回答