2

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.

  • I might come back and revisit this a little later, but let me give an outline for future visitors. The config init-parameter of FilterDispatcher in web.xml is what tells a web-app which .xml files to look for. Assuming use of spring-mock api, you create a MockFilterConfig option. You set the init params for this FilterConfig object (config is the param with the names of your xml files) and then init a FilterDispatcher with this FilterConfig. Could be a step more, but this sounds pretty close. – demongolem Jun 25 '12 at 22:04

1 Answers1

5

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.

WebBowser
  • 66
  • 1
  • 2