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:

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.