Two Laptop Bag
The moose likes Testing and the fly likes Exception testing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » Testing
Reply Bookmark "Exception testing" Watch "Exception testing" New topic
Author

Exception testing

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

How do I test exceptions using Junit?
[ March 10, 2004: Message edited by: Pradeep Bhat ]

Groovy
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Do you mean test your "MyException extends Exception" class, or test that your code throws an exception as expected?
For the former, I usually test only that the stack trace the exception produces contains the pieces of information I expect it to contain (i.e. the most often the description given to the constructor at creation time).
Something like:
For the latter:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

I was talking about the latter one. Thanks for your help.
Jonatan Romero
Greenhorn

Joined: Mar 15, 2004
Posts: 4
[ this post has been edited to remove the blatant advertising. Please visit the advertisement here ]
[ March 15, 2004: Message edited by: Jessica Sant ]
Jonatan Romero
Greenhorn

Joined: Mar 15, 2004
Posts: 4
For an example of how you would perform unit testing with Jtest, imagine that you have just written the fol-
lowing Simple class in your IDE and you want to test it.
Simple.java

To start the test, you tell Jtest to run the Generate and Run Unit Tests test scenario. Jtest examines the
internal structure of the class under test, automatically generates and executes test cases designed to
fully test the class's construction, then determines whether each test case's inputs would produce an
uncaught runtime exception. In seconds, Jtest's test cases cover 100% of the class and identify one
StringIndexOutOfBounds exception.
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ March 15, 2004: Message edited by: Jessica Sant ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Exception testing