Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test methods that throw an exception

 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The test method shown by Lasse will pass when an exception is thrown, and fail when the exception is not thrown.
 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic