A.2. JUnit assertions

As you’ve seen in listing A.1, JUnit provides a set of common assertions as public static methods in the org.junit.Assert class. It’s a common practice to make static imports for these methods to make their use more concise.

There are assertions for checking whether two objects are equal, checking whether an object reference is null, whether two object references point to the same object, whether a condition is true or false, and so forth. The full API of available assertions can be perused online at www.junit.org but here’s a quick overview:

  • assertEquals— Asserts that two objects (or primitives) are equal.
  • assertArrayEquals— Asserts that two arrays have the same items.
  • assertTrue— Asserts that a statement is true.
  • assertFalse— ...

Get Effective Unit Testing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.