Chapter 3. Class junit.framework.Assert

The Assert class has a bunch of static convenience methods to help you with individual test expressions. For example, without JUnit I might code

    if (obtainedResult == null || !expectedResult.equals(obtainedResult))
        throw new MyTestException("Bad output for # attempt");

With JUnit, you can code

    Assert.assertEquals("Bad output for # attempt",
                        expectedResults, obtainedResults);

There are lots of assert* methods to take care of the several common Java types. They take care of checking for nulls. assert() failures (and failure()s, which are the same thing) throw Throwables (probably RuntimeExceptions), which is just what you want to happen. If a test expression fails, that test method quits, cleanup is done, and the next test method in the sequence starts (with the error being noted). There are also static failure methods in the Assert class. They do the same thing as a failed assert()-- they throw a Throwable and fail the current test method. Take a look at the API spec for junit.framework.Assert. Enough said about Assert.