开发者

Struts 2 JUnit plugin and multiple struts configuration files

After several hours surfing the web looking for an answer to this issue, finally I decided to post the question here.

I'm using the struts 2 junit plugin to test some actions of a struts 2 application. The main struts config file (struts.xml) is something like this:

<struts>
    <package name="default" extends="struts-default">

    </package>

    <include file="/com/jmc/textil/productcard/action/productcard.xml"/>
    <!--More includes...-->

</struts>

The productcard.xml file:

<struts>
        <!--Some other packages...-->

        <package name="productClasification" namespace="/productClasification" 开发者_运维百科extends="default">
        <!--More actions...-->
            <action name="edit" method="edit" class="com.jmc.textil.productcard.action.ProductClasificationAction">
                <result>/main/jsp/json_struts2.jsp</result>
            </action>
        </package>

    </struts>

I have a test class that extends StrutsTestCase, and a test method for "/productClasification/edit" action. When the following call is made:

 ActionProxy proxy = getActionProxy("/productClasification/edit.action");
 ProductClasificationAction clasificationAction = 
                        (ProductClasificationAction) proxy.getAction();

an exception is thrown because the action could not be located. By default, StrutsTestCase uses struts.xml but what about other struts configuration xml files?

Thanks in advance.


So I just got done spending the past 5 hours looking at this very issue, and I've finally found a solution. Basically, you need to override the StrutsTestCase setUp method, and add a new StrutsXmlConfigurationProvider for each config file you want to add. Note that the config provider is smart enough to traverse through include statements, so if your configuration "tree" has only one root, you'll only need to add in the root.

sample implementation:

@Override
public void setUp() throws Exception {
    super.setUp();
    List<ContainerProvider> providers = super.configurationManager.getContainerProviders();
    //make one of these for each config file you want to add
    StrutsXmlConfigurationProvider newConfig = new StrutsXmlConfigurationProvider("src/main/webapp/WEB-INF/classes/struts.xml", true, super.servletContext);
    providers.add(newConfig);
    super.configurationManager.reload();
}

If you have many actions in your project, it's probably worth your time to create a base test class that extends StrutsTestCase and just have all your action tests extend that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜