5

我正在为我的应用程序使用 Struts 2.3.14...我需要在我的应用程序中进行一些单元测试...所以我使用的是 junit4...但是当我ActionProxy在测试中使用时,它会显示NullPointerException...

我的测试代码是:

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

我的行动课是:

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

我如何向动作类发送请求以及如何使用ActionProxy......请帮助我改进......提前谢谢......

我的 struts.xml 文件如下所示.....

<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>

下面给出了我导入的 xml 文件....

<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>

我的堆栈跟踪....

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
4

1 回答 1

1

您正在使用junit4,因此在这种情况下使用的正确测试用例类将是StrutsJUnit4TestCase。它具有在启动时运行所需的注释,以初始化运行代码所需的模拟对象。特别是获取动作代理需要 mock request

于 2013-05-20T14:20:33.550 回答