• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simulating Exceptions?

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Is it possible to simulate exception scenarios in Java? I have a task where I am to write JUnit test cases for a code with several catch blocks, with a variety of exceptions handled. In order to have a decent figure for code-coverage, I need to test these catch blocks also.

any suggestions??

TIA
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the exception you'd like to have thrown is expected to be thrown from a method call on a different object inside the try block, you can use mock objects (see jMock and EasyMock) to replace the object receiving the method call.

For example, UserDao.find(name) throws UserNotFoundException when it doesn't find the given name in the database. You want to test that LoginAction.login(name, password) returns false when the name doesn't match a User.Normally you'd hook LoginAction up to a real UserDao that connects to your database. Using mocks, however, you'd hook it up to a mock UserDao. Your test case would look something like this:If it means anything to you, I chose jMock because I found its API more expressive and was up and using it in under a day after learning about mock objects.

YMMV, but I highly recommend using mocks.
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot david.

What I am typically interseted in is that is there a way to simulate exceptions like IOException , BufferOverFlowException , PrinterException , and other such exceptions that are usually system generated ones (and not due to programmer errors)

Any suggestions?
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ashish Chopra:
Thanx a lot david.

What I am typically interseted in is that is there a way to simulate exceptions like IOException , BufferOverFlowException , PrinterException , and other such exceptions that are usually system generated ones (and not due to programmer errors)

Any suggestions?



You could still use Mock Objects for those types of exceptions.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a great example of the need to test driving the design to a good place. Ok, "good" is subjective but let's see:

That's going to be very hard to mock because you're hard coding a library class - maybe a file reader - in there.

Now I have control in MyApplicationClass and maybe I could have a testing switch to tell it to throw an exception.

Now I can plug in a mock helper that throws exceptions just for this test. My class under test no longer has a dependency on any class, just an interface. Reducing dependencies is a "good" thing, adding configurable flexibility might be a useful thing, too.

Another approach might be to move all the complex catch logic you want to do to another class and test it independently.

Now helper has a public method I can test.

Hope some of that rang some bells and looks useful.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Testing...
 
reply
    Bookmark Topic Watch Topic
  • New Topic