public inbox for frysk@sourceware.org
 help / color / mirror / Atom feed
* test case, exceptions, and tearDown
@ 2007-07-03 21:52 Andrew Cagney
  2007-07-03 23:05 ` Kris Van Hees
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Cagney @ 2007-07-03 21:52 UTC (permalink / raw)
  To: frysk

Just FYI,

Todays IRC had a bit of discussion regarding how a JUnit test is written 
(as a style thing). I've found the JUnit faq ( 
http://junit.sourceforge.net/doc/faq/faq.htm ), even though it is using 
version 4 code, makes for a good reference for how the authors intended 
the framework to be used.

Two answers I found useful:

> *How do I write a test that fails when an unexpected exception is 
> thrown?*
>
> Declare the exception in the |throws| clause of the test method and 
> don't catch the exception within the test method. Uncaught exceptions 
> will cause the test to fail with an error.
>
> The following is an example test that fails when the 
> |IndexOutOfBoundsException| is raised:
>
> |
>
>     @Test
>     public void testIndexOutOfBoundsExceptionNotRaised() 
>         throws IndexOutOfBoundsException {
>     
>         ArrayList emptyList = new ArrayList();
>         Object o = emptyList.get(0);
>     }
>       |
Notice how the exception doesn't need to be explicitly caught; instead 
the exception being thrown is interpreted as a FAIL.

and:

> *When are tests garbage collected?*
>
> /(Submitted by: Timothy Wall and Kent Beck)/
>
> By design, the tree of Test instances is built in one pass, then the 
> tests are executed in a second pass. The test runner holds strong 
> references to all Test instances for the duration of the test 
> execution. This means that for a very long test run with many Test 
> instances, none of the tests may be garbage collected until the end of 
> the entire test run.
>
> Therefore, if you allocate external or limited resources in a test, 
> you are responsible for freeing those resources. Explicitly setting an 
> object to |null| in the |tearDown()| method, for example, allows it to 
> be garbage collected before the end of the entire test run.
>
Things involving file-descriptors would certainly fall into that 
category.  Something to keep an eye out for :-)


Andrew

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-07-05 18:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-03 21:52 test case, exceptions, and tearDown Andrew Cagney
2007-07-03 23:05 ` Kris Van Hees
2007-07-05 15:52   ` Andrew Cagney
2007-07-05 16:28     ` Kris Van Hees
2007-07-05 18:04       ` Andrew Cagney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).