Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Testing for exceptions

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I've written a class that copys a text file to a new file with a new name using:



And part of my task is to write a unit test for this which includes testing for IOExceptions.
I'm not sure how you test for exceptions.
I usually have a class that extends TestCase,
then setUp() method, and some test methods like testSomething().

I usually then use the assertEquals etc. etc. methods to test stuff.
Well, we can see the transferFrom(_,_) method throws an IOException,
but I have no idea how to test for this

Any ideas?
Thanks for any help
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Colin,

I've heard this question so many times in a short time that I decided to blog about it so I can refer to my blog instead of explaining it multiple times:

http://radio.javaranch.com/lasse/2007/05/17/1179405760728.html

Does the blog entry respond to your question sufficiently?
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much, that helps a lot.
I'm using JUnit 3, so I've been using the first one.
So basically Assert.fail() is expecting an exception, so if an exception is thrown, then Assert.fail() will pass since it has found an exception.

And if you use Assert.fail() with code that is correct, then testCase will fail.

Thanks, I think I get it now
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, I was just thinking,
Using Assert.fail() makes the test case pass when an exception is thrown.

But perhaps it would make more sense if the test case method failed when an exception is thrown, how can this be done?

Thanks for any help
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by colin shuker:

I'm using JUnit 3, so I've been using the first one.
So basically Assert.fail() is expecting an exception, so if an exception is thrown, then Assert.fail() will pass since it has found an exception.

And if you use Assert.fail() with code that is correct, then testCase will fail.


Assert.fail() will always throw a org.junit.AssertionFailedError (or the equivalent from "junit.framework.*" when using JUnit 3) when it is invoked.

I've tried to make the example in my blog more clear with some additional comments:


In other words, if fail() executes, then an AssertionFailedError is thrown and it propagates directly out from the test method because the try-catch only captures NumberFormatExceptions. If a NumberFormatException is thrown as we expect the code to do, then the catch block captures the exception and we ignore it because that behavior is exactly what we want.

Does this make it more clear or am I just making it more confusing?
[ May 17, 2007: Message edited by: Lasse Koskela ]
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeh, I think I get it, but my point is...
I have to test for an IOException, so I assumed I could somehow get a test to fail if there was an exception as opposed to what fail() does, where it allows the test to pass if theres an exception.

It seems more natural to get a failed test if you do get an exception.

Any thoughts?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by colin shuker:
It seems more natural to get a failed test if you do get an exception.


Well, if the exception is the desired behavior then the test should pass.
If the exception should not happen, then just let the exception propagate out from the test method and JUnit will flag that test as a failure.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If the exception should not happen, then just let the exception propagate out from the test method and JUnit will flag that test as a failure.



So you mean.. don't use a try/catch block, and let the test fail if an exception is thrown?

My question is...
Copy a file to a new file with a new name, then
write a unit test for this which includes testing for IOExceptions.

So in the context of the question, I don't know what the is the best thing to do regarding the testing for IOExceptions.

Do you think I should use Assert.fail() and thus fail the test is there is no exception, or just let the test fail if it gets an exception?

Thanks for any advice

[ May 18, 2007: Message edited by: colin shuker ]
[ May 18, 2007: Message edited by: colin shuker ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by colin shuker:
Do you think I should use Assert.fail() and thus fail the test is there is no exception, or just let the test fail if it gets an exception?


In those cases where you don't want the copy method to throw an IOException, don't catch it.

In those cases where you want the copy method to throw an IOException, catch it and fail() if no such exception is thrown.

In other words, you will need to do both.
[ May 20, 2007: Message edited by: Lasse Koskela ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic