Struts 2.3.14 junit test

こ雲淡風輕ζ 提交于 2021-02-07 14:13:01

问题


I'm using Struts 2.3.14 for my application... I need some unit test in my application... so I'm using junit4... but when I use ActionProxy in my test, it will show NullPointerException...

my test code is:

ActionProxy proxy=getActionProxy("/customize/saveRewards");
    SaveRewardsPlus rewards=(SaveRewardsPlus)proxy.getAction();
    request.setParameter("list[0].img_id", "1");        
    request.setParameter("list[0].tag", "Test Tag");
    request.setParameter("list[0].desc", "Test Desc");        
    String result=proxy.execute();        
    assertEquals("success", result);  

and my Action Class is:

private List<RewardsBean> list;    

public String execute() {
    try {
        Class.forName(DRIVER);
        con = DriverManager.getConnection(CONSTR, USER, PWD);
        for (RewardsBean rb : list) {
            sql = "update rewards set tag=?,description=? where img_id=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, rb.getTag());
            ps.setString(2, rb.getDesc());
            ps.setInt(3, rb.getImg_id());
            ps.executeUpdate();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return SUCCESS;
}

/**
 * @return the list
 */
public List<RewardsBean> getList() {
    return list;
} 

How can i send request to the action class and how to use ActionProxy... Please Help me to Improve..... Thankyou in advance......

my struts.xml file is given below.....

<struts>
  <!-- Configuration for the default package. -->
  <package name="default" extends="struts-default">
    <action name="addRewards" class="com.customize.rewards.AddRewards">
       <result name="success">index.jsp</result>
    </action>
  </package>
  <include file="addrewards/struts-addrewards.xml"></include>
  <include file="customize/struts-customize.xml"></include>
</struts>

and my imported xml file is given below....

<struts>
    <package name="customize" namespace="/customize" extends="struts-default">
        <!--<action name="viewRewards" class="com.customize.rewards.ViewRewards">
            <result>/customizeRewards/viewRewards.jsp</result>
        </action>        -->
        <!-- New Rewards-->
        <action name="viewRewards" class="com.customize.rewards.ViewRewards">
            <result>/customizeRewards/viewRewardsPlus.jsp</result>
        </action>
        <action name="saveRewards" class="com.customize.rewards.SaveRewardsPlus"> 
            <result name="success" type="redirect">viewRewards</result>           
        </action>
        <!--/New Rewards-->
       <!-- <action name="saveRewards" class="com.customize.rewards.SaveRewards"> 
            <result name="success" type="redirect">viewRewards</result>           
        </action>-->
        <action name="deleteRewards" class="com.customize.rewards.DeleteRewards">
            <result name="success" type="redirect">viewRewards</result>
        </action>
        <action name="addRewards" class="com.customize.rewards.AddRewards">
            <result name="success" type="redirect">viewRewards</result>
        </action>
    </package>
</struts>

my stacktrace....

java.lang.NullPointerException at
org.apache.struts2.StrutsTestCase.getActionProxy(StrutsTestCase.java:130) at 
com.customize.rewards.SaveRewardsPlusTest.testExecute(SaveRewardsPlusTest.java:4‌​9) 
Testsuite: com.customize.rewards.SaveRewardsTest

回答1:


You are using junit4, so the correct test case class to use in this case would be StrutsJUnit4TestCase. It has annotations required to run on start up to init mock objects necessary to run your code. Particular getting action proxy requires the mock request.



来源:https://stackoverflow.com/questions/16621412/struts-2-3-14-junit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!