how to test exception condition in Junit

View: New views
2 Messages — Rating Filter:   Alert me  

how to test exception condition in Junit

by Sushil Dodake :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i am trying to write a test case for the exception scenario.
my code looks as below.
 

public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {

ActionForward actionforward =

null;

try{

SimpleDateFormat format =

new SimpleDateFormat(ReportingConstant.DATE_FORMAT);

ReportRequest reportReq =

new ReportRequest();

String distID = request.getParameter(

"distId");

String beginDate = request.getParameter(

"beginDate");

String endDate = request.getParameter(

"endDate");

if(null != beginDate){

reportReq.setBeginDate(format.parse(beginDate));

}

else{

LOG.error("Begin date not present in the received form ");

throw new ReportingException("Begin date not present in the received form");

}

if(null != endDate){

reportReq.setEndDate(format.parse(endDate));

}

else{

LOG.error("End date not present in the received form ");

throw new ReportingException("End date not present in the received form");

}

if(null != distID){

reportReq.setDistributorId(distID);

}

else{

LOG.error("Distributor Id not present in the received form ");

throw new ReportingException("Distributor Id not present in the received form ");

}

some code....

}

catch
(Exception e){

LOG.error("Error sending XLS file to the client ",e);

actionforward = mapping.findForward(

"exception");

}

return actionforward;

}

 

now i would like to test a execute method for exception i.e. i am expecting that if the request obj do not have any parameters set then my code will throw an exception.

and i need to verify that.

my test case method is like..

public
void testDownloadXLSReportFailure()throws Exception{

httpServletRequestMock.expects(once())

.method(

"getParameter")

.with(eq(

"distId"))

.will(returnValue(

null));

request

= (HttpServletRequest)httpServletRequestMock.proxy();

try{

ActionForward forward =

downloadReportAction.execute(actionMapping, reportForm, request, response);

fail(

"Error ..");

}

catch(Exception e){

}

}

org.jmock.core.DynamicMockError: mockHttpServletRequest: unexpected invocation
Invoked: mockHttpServletRequest.getParameter("beginDate")
Allowed:
expected once and has been invoked: getParameter( eq("distId") ), returns null

 at org.jmock.core.AbstractDynamicMock.mockInvocation(AbstractDynamicMock.java:58)
 at org.jmock.cglib.CGLIBCoreMock.intercept(CGLIBCoreMock.java:71)
 at $javax.servlet.http.HttpServletRequest$$EnhancerByCGLIB$$7c120c24.getParameter(<generated>)
 at com.toro.rebate.reporting.struts.action.DownloadXLSCommercialReportAction.execute(DownloadXLSCommercialReportAction.java:47)
 at com.toro.rebate.reporting.struts.action.DownloadXLSCommercialReportActionTest.testDownloadXLSReportFailure(DownloadXLSCommercialReportActionTest.java:116)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at junit.framework.TestCase.runTest(TestCase.java:154)
 at org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:39)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:118)
 at junit.framework.TestSuite.runTest(TestSuite.java:208)
 at junit.framework.TestSuite.run(TestSuite.java:203)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

can sombody tell me how do i chk the exception condition here?

 

thanks,

sush


Re: how to test exception condition in Junit

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This message

org.jmock.core.DynamicMockError: mockHttpServletRequest: unexpected  
invocation
Invoked: mockHttpServletRequest.getParameter("beginDate")
Allowed:
expected once and has been invoked: getParameter( eq("distId") ),  
returns null

is telling you that you called getParameter() but didn't set up either  
a stub or expectation, so the mock doesn't know what to do.


S.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price!