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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

13.5 Writing Test Cases307• assertTrue( boolean expressi<strong>on</strong> )• assertFalse( boolean expressi<strong>on</strong> )• assertNull (Object)• assertNotNull (Object)• assertSame (Object1, Object2)• assertNotSame (Object1, Object2)• fail()Each of the assert methods comes in two “flavors,” <strong>on</strong>e with a messageString and <strong>on</strong>e without. For example, there is a method assertTrue() whichtakes a boolean as its parameter; typically it would be used with an expressi<strong>on</strong>,for example: 1assertTrue( (sample actual) );If the c<strong>on</strong>diti<strong>on</strong> is not true, an Asserti<strong>on</strong>FailedError is thrown. Thatmeans, am<strong>on</strong>g other things, that if/when your test fails, it will stop executingat that point. The tearDown() method, though, will still be executed beforeproceeding to the next test.There is also a method of the same name, assertTrue(), but with aslightly different signature—it adds a String as its first parameter. The stringis the message to be included in the error report. Using this variati<strong>on</strong> <strong>on</strong>assertTrue(), our example would become:assertTrue("Sample too small", (sample actual));In the same way, assertFalse() has two versi<strong>on</strong>s—assertFalse(boolean) and assertFalse(String, boolean)—and so<strong>on</strong> for all other assert methods.1. Yes, the extra parentheses are not needed; they just make the point that this is a boolean expressi<strong>on</strong>being passed as the argument to assertTrue(). We could also have written it as:boolean result = (sample actual);assertTrue(result);Again, the extra parentheses are used just to make it clearer.

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

Saved successfully!

Ooh no, something went wrong!