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:49)
Testsuite: com.customize.rewards.SaveRewardsTest
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With