aspose file tools*
The moose likes Testing and the fly likes Test methods that throw an exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Test methods that throw an exception" Watch "Test methods that throw an exception" New topic
Author

Test methods that throw an exception

Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Hi,



I am trying to figure out how to "Test methods that throw an exception". The
test pattern is taken from "AsyncAppenderTestCase.java" (below)






Here is an example:



Now, in this try/catch block, logger.debug("m2"); would throw an exception,
now what I need to do is:



1. Catch this exception, Since I have the text for the exception:

log4j:WARN No appenders could be found for logger
(com.webmethods.sc.logging.test.unit.WmLoggerFilterTest). log4j:WARN Please
initialize the log4j system properly.

2. Fail the test
3. Compare the exception thrown by logger.debug("m2"); with the actual
excption
4. if same , pass the test
5. if not, then fail the test



Thanks,

-Kamal.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
It's up to you whether you feel the need to test for an expected exception message, for an expected exception class, or for just "any" exception being thrown.

You seemed to have the right pattern already from what I could tell from the code snippets you posted:


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

Joined: Feb 15, 2005
Posts: 91
Lasse,

Thanks for your response, what i want to happen , is for the test to pass, which currently it does not. Any idea how i could pass the test, since my argument is that since the "expected" exception is thrown, then the "TEST" passes.
Thanks,
-Kamal.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The test method shown by Lasse will pass when an exception is thrown, and fail when the exception is not thrown.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Got it,

So, while trying to debug the try /catch block, i added some println's



and i get the following on Console:

one
oneprime
log4j:WARN No appenders could be found for logger (com.webmethods.sc.logging.test.unit.WmLoggerFilterTest).log4j:WARN Please initialize the log4j system properly.

Hence , as per your comments, am i correct to conclude that although the required behaviour is:
When i close the appender with wmDfa.close(); , the logger sould not be able to write to a closed appender, but in this case the close() is not closing the appender, and hence the message on the console.

Now just one more question:
The message i see on the console:
log4j:WARN No appenders could be found for logger (com.webmethods.sc.logging.test.unit.WmLoggerFilterTest).log4j:WARN Please initialize the log4j system properly.

is it the Exception ? or is it e.getMessage() ? or an error
How can i find out if there IS an exception being thrown.

Thanks,
-Kamal.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Kamal Ahmed:
Hence , as per your comments, am i correct to conclude that although the required behaviour is:
When i close the appender with wmDfa.close(); , the logger sould not be able to write to a closed appender, but in this case the close() is not closing the appender, and hence the message on the console.

The "debug()" call after "close()" is not throwing an exception.
It's up to you to decide whether that's the correct behavior or not.

Originally posted by Kamal Ahmed:
Now just one more question:
The message i see on the console:
log4j:WARN No appenders could be found for logger (com.webmethods.sc.logging.test.unit.WmLoggerFilterTest).log4j:WARN Please initialize the log4j system properly.

is it the Exception ? or is it e.getMessage() ? or an error
How can i find out if there IS an exception being thrown.

That's not an exception nor an exception's message. It's written to System.err by the Log4J logging framework because it doesn't find any appenders for a logger named "com.webmethods.sc.logging.test.unit.WmLoggerFilterTest".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Test methods that throw an exception