|
Re: Re: distinguishing between failures and errors: msg#00142java.junit.user
pholser wrote: >--- In junit@xxxx, Mike Clark <mike@xxxx> wrote: > >>In the following example, the IndexOutOfBoundsException is expected >>and checked with an assertion. If the expected exception is not >>raised, then a failure is produced. >> > >simplifying (the ctor that takes an int kind of misdirects): > >public void testExceptionExpected() { > try { > new ArrayList().get( 23 ); > fail( "Should have thrown IndexOutOfBoundsException" ); > } > catch ( IndexOutOfBoundsException success ) { > } >} > >how about changing the example so that IndexOutOfBoundsException >shouldn't be thrown. plus, IndexOutOfBoundsException is an unchecked >exception, so don't bother with adding it to the throws clause: > > public void testExceptionUnexpected() { > List list = new ArrayList(); > list.add( "blah" ); > assertEquals( "0th element", "blah", list.get( 0 ) ); > } >[snip > Good feedback! Indeed, it wasn't as clear as I intended. Here's another attempt at that FAQ. I've chosen to test some IO classes that can possibly throw checked exceptions. This demonstrates that unexpected checked exceptions are declared in the 'throws' clause of the test method. It also tries to clarify what happens when unchecked exceptions are raised. Is this clearer? ---- Q: What's the difference between a failure and an error? A: Assertions are used to check for the possibility of failures, therefore failures are anticipated. Errors are unanticipated problems resulting in uncaught exceptions being propagated from a JUnit test method. In the following example, the FileNotFoundException is expected and checked with an assertion. If the expected exception is not raised, then a failure is produced. If any other unexpected IOException or unchecked exception (e.g. NullPointerException) is raised, the JUnit framework catches the exception and signals an error. public void testNonexistentFileRead() throws IOException { try { File file = new File("doesNotExist.txt"); FileReader reader = new FileReader(file); assertEquals('a', (char)reader.read()); fail("Should have thrown FileNotFoundException"); } catch (FileNotFoundException success) {} } In the following example, an IOException is not expected. The JUnit framework will signal an error if an IOException (e.g. FileNotFoundException) or any unchecked exception (e.g. NullPointerException) is raised. public void testExistingFileRead() throws IOException { File file = new File("exists.txt"); FileReader reader = new FileReader(file); assertEquals('a', (char)reader.read()); } Both failures and errors will cause the test to fail. However, it is useful to differentiate between failures and errors because the debugging process is slightly different. In the first example, the use of fail() will not generate a complete stack trace including the method that raised the exception. In this case that's sufficient since we anticipate that the exception will be raised. If it's not raised, then it's a problem with the test itself. In the second example, the JUnit framework catches the exception and generates an error with a complete stack trace for the exception. Since we don't expect this exception to be raised, a complete stack trace is useful in debugging why it was raised. ---- Mike [Non-text portions of this message have been removed]
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: testing web application (jsp) with JavaScript, Kirill Maximov |
|---|---|
| Next by Date: | FAQ entry critique (Re: Re: distinguishing between failures and errors), J. B. Rainsberger |
| Previous by Thread: | Re: distinguishing between failures and errors, pholser |
| Next by Thread: | FAQ entry critique (Re: Re: distinguishing between failures and errors), J. B. Rainsberger |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |