• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

JUnit in action: does it cover how to handle exceptions correctly?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there! I have a question to the author: does your book cover how to correctly test methods that throw exceptions?
I've testing methods that throw exceptions like this:
- at first, I simply write a testXXX() throws Exception{}
- then, I write code that should throw an exception, an I catch it. But the testXXX() still throws exceptions, so I can test that my method really is throwing the right exception.
Is this procedure ok, or is there any other ideias in the book? Thanks for any reply!
I think exceptions are a very important issue, and something not that easy to test sometimes.
[ November 06, 2003: Message edited by: bengt hammarlund ]
 
author & internet detective
Posts: 39664
785
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bengt,
If the method could throw an exception, but you don't expect it to, testXXX() throws Exception{} is fine. If you want to test that the code throws a certain exception, you can use code of the form.
 
Author
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by bengt hammarlund:
hi there! I have a question to the author: does your book cover how to correctly test methods that throw exceptions?
I've testing methods that throw exceptions like this:
- at first, I simply write a testXXX() throws Exception{}
- then, I write code that should throw an exception, an I catch it. But the testXXX() still throws exceptions, so I can test that my method really is throwing the right exception.
Is this procedure ok, or is there any other ideias in the book? Thanks for any reply!
I think exceptions are a very important issue, and something not that easy to test sometimes.
[ November 06, 2003: Message edited by: bengt hammarlund ]


Yes the book does cover it. Actually in most places you'll find that it shows how to test for positive and error paths. What you're doing seems right. There are several other best practices as discussed in the book, like use "expected" as the name of the exception you're expecting, etc.
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vincent,
Haven't used JUnit yet, just getting started. Which type of testing wud you suggest, is it GUI based or Text based (customizing Ant scripts).
Thanks.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Anil Vupputuri:
Which type of testing wud you suggest, is it GUI based or Text based (customizing Ant scripts).

Both, I'd say. Have your Ant script run all the tests when making a build, and run a subset of the tests from your IDE while developing.
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!