Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception testing

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I test exceptions using Junit?
[ March 10, 2004: Message edited by: Pradeep Bhat ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was talking about the latter one. Thanks for your help.
 
Jonatan Romero
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ 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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic