How do i test a method which returns NullPointerException to my JUnit testCase below:
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   voruganti_srinivas
Posted On:   Tuesday, March 11, 2003 02:31 AM

Here is my JUnit testCase : public void testGetUserData(){ try{ dao_viewUserDAO = obj_userManager.getUserData( ); System.out.println("----User EmailId---"+ dao_viewUserDAO.str_emailId); assertTrue("User exists", dao_viewUserDAO.str_emailId!= null); } catch(Exception e){ e.printStackTrace(); } } Here i will pass null data or non existing data as a parameter to "obj_userManager.getUserData()" method. And when i run this testcase, it is giving java.lang.NullPointerException. Thanks & Regards, srinu.    More>>

Here is my JUnit testCase :


public void testGetUserData(){
try{
dao_viewUserDAO = obj_userManager.getUserData( );
System.out.println("----User EmailId---"+
dao_viewUserDAO.str_emailId);
assertTrue("User exists", dao_viewUserDAO.str_emailId!= null);
}
catch(Exception e){
e.printStackTrace();
}
}


Here i will pass null data or non existing data as a parameter to "obj_userManager.getUserData()" method.
And when i run this testcase, it is giving java.lang.NullPointerException.



Thanks & Regards,
srinu.    <<Less

Re: How do i test a method which returns NullPointerException to my JUnit testCase below:

Posted By:   Peter_Craddock  
Posted On:   Wednesday, April 30, 2003 07:56 AM

From this I assum you want a null pointerexception to be thrown. If this is true I would test as follows.



boolean result = false;



try

{

test methods ..
}

catch(NullPointerException npe)

{

result = true;

}

catch(Exception e)

{

//dump stack

fail();

}



assertTrue(result);



hope this helps pete.

Re: How do i test a method which returns NullPointerException to my JUnit testCase below:

Posted By:   Anonymous  
Posted On:   Thursday, March 13, 2003 01:53 AM

I am not sure I understand your question. Do you *want* the NullPointerException to be thrown and the test let assert that?

If so, take a look at this FAQ...

About | Sitemap | Contact