2

I need to write jUnit for Struts 2 action class.But action class is implementing ParameterAware interceptor which takes parameter map in HashMap and action class takes all request parameter from this hashmap

Action Class:

public class BrokerAction extends ActionSupport implements ServletRequestAware, ServletResponseAware,SessionAware,
        ParameterAware {

    /** The parameters. */
    private Map parameters;

    @Override
    public void setParameters(final Map param) {
        this.parameters = param;
    }

   public String getParameterValue(final String param) {
        final Object varr = getParameters().get(param);
        if (varr == null) {
            return null;    
        }
        return ((String[]) varr)[0];
    }

   public String getBrokerDetails()throws PactException{
        String id = getParameterValue("id");
   }

When i am putting data from Junit in request then it hasn't reflected in parameters map

Junit:

@Test
     public void testGetActionProxy() throws Exception {
        
         request.setAttribute("id", "23");
         ActionProxy proxy   =  getActionProxy("/getBrokerDetails");
         BrokerAction brokerAction = (BrokerAction) proxy.getAction();
         brokerAction.getBrokerDetails();
    }

Please help as how request data will be reflected in parameters map

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3381610
  • 55
  • 1
  • 6

2 Answers2

0

Unit tests should be simple - instead of setting attributes on a request and using ActionProxy, why not just call setParameters to set your parameters? If your unit test is calling out to code that isn't in the class you are testing, then it becomes more complex and prone to failure for reasons not related to your code.

codebox
  • 19,927
  • 9
  • 63
  • 81
  • Thanks for the response.That what i am doing currently but was curious that if action implements ParameterAware or SessionAware, which are struts inteceptor, so can we test inteceptor chain also via junit? – user3381610 Mar 10 '14 at 10:07
  • There's also unclear which `setParameters` to call, so this is not an answer. – Roman C Mar 10 '14 at 11:00
  • There is only 1 method called setParameters in the provided code, I think its pretty clear – codebox Mar 10 '14 at 11:24
  • @codebox you should not call *setParameters in the provided code*, because it's already done via servlet config interceptor. – Roman C Mar 10 '14 at 11:35
  • I don't think a true unit test should be running things like interceptors, that's more a job for integration testing. A unit test should run the smallest unit of code possible, there is no need to involve framework code in this example. – codebox Mar 10 '14 at 12:06
0

The code to test the request parameter

<package name="test" namespace="/" extends="struts-default">
    <action name="getBrokerDetails" class="BrokerAction ">
        <result>/page.jsp</result>
    </action>
</package>


 public void testGetActionProxy() throws Exception {

     request.setParameter("id", "23");
     ActionProxy proxy   =  getActionProxy("/getBrokerDetails");
     BrokerAction brokerAction = (BrokerAction) proxy.getAction();

     String result = proxy.execute();
     assertEquals(Action.SUCCESS, result);

     assertEquals("23", brokerAction.getBrokerDetails());

}
Roman C
  • 49,761
  • 33
  • 66
  • 176