6.4 Test Assert Methods

     

6.4 Test Assert Methods

A range of test assert methods are provided by JUnit. They are implemented as public static methods of the class Assert , which is a parent class of TestCase . Thus, every test class inherits these methods.

The most generic test assert method is assertTrue() , which simply passes or fails based on the value of a Boolean argument. The other test assert methods are specialized versions of assertTrue( ) that handle particular types of test conditions. For example, the following test assert statements are equivalent:

 assertTrue( book.title.equals("Cosmos") ); assertEquals( "Cosmos", book.title ); 

These statements are equivalent as well:

 assertTrue( false ) fail( ) 

The specialized test assert methods are useful because they save coding effort, are easier to read, and allow more specific reporting of the results.

The assert methods all have two variants, one that takes a String message as the first argument, and one that doesn't. The message allow you to provide a more detailed description of an assertion failure.

The assertEquals() methods compare the values of two arguments. They assume that the first value is the correct or "expected" value to which the second "actual" value should be compared. These methods will work if the arguments are reversed , but the failure message will be misleading.

The JUnit assert methods are described in the following list:


static void assertTrue(boolean condition)

static void assertTrue(message, boolean condition)

The assertTrue assertion passes if condition is true . This is the most generic type of assertion.


static void assertFalse(boolean condition)

static void assertFalse(message, boolean condition)

Test passes if condition is false .


static void assertEquals(expected, actual)

static void assertEquals(message, expected, actual)

Test passes if expected and actual are equal. Versions of this method exist to compare arguments of type boolean , byte , char , int , long , Object , short , or String .


static void assertEquals(expected, actual, delta)

static void assertEquals(message, expected, actual, delta)

Asserts equality of two values within a tolerance of delta . A delta of 0.0 tests exact equality. Versions of this method exist to handle arguments of type double or float .


static void assertNotNull(Object object)

static void assertNotNull(message, Object object)

Test passes if Object is not null .


static void assertNull(Object object)

static void assertNull(message, Object object)

Test passes if Object is null .


static void assertNotSame(Object expected, Object actual)

static void assertNotSame(message, Object expected, Object actual)

Test passes if the two Object s are not the same Object, as determined by the == operator .


static void assertSame(Object expected, Object actual)

static void assertSame(message, Object expected, Object actual)

Test passes if the two Object s are the same Object, as determined by the == operator .


static void fail( )

static void fail(message)

Test that always fails. It is equivalent to assertTrue(false) .



Unit Test Frameworks
Unit Test Frameworks
ISBN: 0596006896
EAN: 2147483647
Year: 2006
Pages: 146
Authors: Paul Hamill

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net