How to test methods that throw exceptions
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Roshani_Pandey
Posted On:   Monday, July 1, 2002 03:22 PM

Hi, I am very new to Junit. So, my problem might sound very basic. But I am totally clueless here...
I am trying to test this method that throws a NullPointerException. Can you test such methods with junit? If I can't test the return value of the method, is there anything else about the method that I can test to ensure that the method is fine?

Re: How to test methods that throw exceptions

Posted By:   Roshani_Pandey  
Posted On:   Tuesday, July 2, 2002 10:27 AM

Actually, the problem is more like this: There is this protected method that I have to test that uses a class variable that has been set to null. However, the variable is set to something else in the public methods that appear prior to the method that I am testing. My problem is that I don't know how to set the variable in the method that I am testing so that it is not null and doesn't give me a NullPointerException. Please help.

Re: How to test methods that throw exceptions

Posted By:   Anonymous  
Posted On:   Tuesday, July 2, 2002 02:24 AM

If you want to test whether an exception is thrown I recommend the FAQ of JUnit (as part of the downloaded zip) "How do I implement a test case for a thrown exception?".


The FAQ-Online is also a good resource. Especially How do I test a method that doesn't return anything? is the right for you.

Re: How to test methods that throw exceptions

Posted By:   Anonymous  
Posted On:   Monday, July 1, 2002 11:45 PM

What is the method supposed to do? Can you test for this effect?

There seems to be the widespread misconception that you have a one to one mapping of tests to methods. That isn't true - a test doesn't necessarily tests a single method but a small usage scenario for an object or even the collaboration of some objects.

For example, one test for the HashMap class could look like


HashMap map = new HashMap()
Object key = new Object();
Object value = new Object();
map.put(key, value);
assertSame(value, map.get(key));


Does that help?

Re: How to test methods that throw exceptions

Posted By:   John_Sampson  
Posted On:   Monday, July 1, 2002 03:35 PM

Check out Jtest from www.parasoft.com
this is off their site
During white-box testing, Jtest tests your code's internal structure at the class level and validates that the class, when passed
unexpected input, will not cause the program to crash. Jtest uses Parasoft's patented Test Generation System technology (patent
#5,784,553 & 5,761,408) to automatically generate input test cases that expose uncaught, undocumented runtime exceptions.
About | Sitemap | Contact