13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

306Chapter 13JUnit: Automating Unit TestingtestCreateSub() is a single JUnit test which asserts various c<strong>on</strong>diti<strong>on</strong>s aboutvarious invocati<strong>on</strong>s of the createSub() method. Note that all of the asserti<strong>on</strong>sencountered in the executi<strong>on</strong> of the test class must pass for the test to pass.So what happens if an asserti<strong>on</strong> fails? The assert method will throw an excepti<strong>on</strong>,reporting the failure. In JUnit terminology, a failure is a test that didn’tpass, whereas an error is a problem with the running of the test. A missing classor a null pointer excepti<strong>on</strong> are errors, whereas an assertNotNull() call failingis c<strong>on</strong>sidered a test failure.The handy thing about the excepti<strong>on</strong>s that the assert methods throw isthat they are, technically speaking, not java.lang.Excepti<strong>on</strong> throwables butrather bel<strong>on</strong>g to the java.lang.Error type of throwable. (D<strong>on</strong>’t c<strong>on</strong>fuse thistechnical Java use of the word “error” with our more informal use in the previousdiscussi<strong>on</strong> of failure versus error.) To quote from the Javadoc page forjava.lang.Error:A method is not required to declare in its throws clause any subclasses ofError that might be thrown during the executi<strong>on</strong> of the method but notcaught, since these errors are abnormal c<strong>on</strong>diti<strong>on</strong>s that should never occur.So the use of Error by JUnit’s various assert methods is d<strong>on</strong>e simply as ac<strong>on</strong>venience for us test developers, so that we d<strong>on</strong>’t have to put throws ...clauses <strong>on</strong> all of our method declarati<strong>on</strong>s.13.5.1 JUnit Asserti<strong>on</strong>sThese are the various test asserti<strong>on</strong>s available with JUnit:• assertEquals(), comparing• boolean with boolean• char with char• short with short• int with int• l<strong>on</strong>g with l<strong>on</strong>g• float with float• double with double• Object with Object• String with String

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!