Posted By:
vasu_pam
Posted On:
Friday, March 21, 2008 02:08 AM
How catch exception from struts action in test class using junit Hi i am using junit in my struts application and my test class extends MockStrutsTestCase. i want to test LoginAction class.For this class,i pass username and password by using addRequestParameter method. if i pass parameters correctly,the code is working fine. If i pass incorrect parameter name (for example instead of uname i give name) or missing any parameters, the java class throws NullPointerException. how can i catch that exception in my test class and display that message. This is the code for that: import servletunit.struts.*; import org.apache.struts.action.*;
More>>
How catch exception from struts action in test class using junit
Hi
i am using junit in my struts application and my test class extends MockStrutsTestCase.
i want to test LoginAction class.For this class,i pass username and password by using addRequestParameter method.
if i pass parameters correctly,the code is working fine.
If i pass incorrect parameter name (for example instead of uname i give name) or missing any parameters, the java class throws NullPointerException.
how can i catch that exception in my test class and display that message.
This is the code for that:
import servletunit.struts.*;
import org.apache.struts.action.*;
public class TestCheckAssociate extends MockStrutsTestCase {
public TestCheckAssociate(String name) {
super(name);
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSuccessfulAsLogin()
{
setRequestPathInfo("/checkassociate");
addRequestParameter("uname","test@gmail.com");
addRequestParameter("password","123456");
actionPerform();
verifyForward("correct");
assertEquals("test@gmail.com",(String) getSession().getAttribute("asusername"));
}
public void testFailedAsLogin()
{
setRequestPathInfo("/checkassociate");
addRequestParameter("uname","test@gmail.com");
addRequestParameter("password","124456");
actionPerform();
verifyForward("incorrect");
assertNull((String) getSession().getAttribute("asusername"));
}
public void testCheckParameters()
{
ActionRedirect ar = new ActionRedirect("/checkassociate.do");
ar.addParameter("name", "test@gmail.com");
ar.addParameter("password", "123456");
actionPerform();
verifyForward("incorrect");
assertEquals("test@gmail.com",(String) getSession().getAttribute("asusername"));
}
}
please help me in this regard.
Regards
vasu
<<Less