JUnit assertions methods

JUnit assertions methods: assertEquals, assertSame, assertNull, assertNotNull, assertTrue, assertFalse explained.

There are several JUnit assertion methods and they are all static methods defined in org.junit.Assert class. You can easily use them if you do import static org.junit.Assert.*;

  • fail() - fails test
  • assertNotNull(o) / assertNull(o) - assert if object is (not) null
  • assertFalse(b) / assertTrue(b) - asserts if b is false / true
  • assertEquals(a, b) - compares two objects using equals() method: a.equals(b)
  • assertSame(a, b) - compares two objects using == operator:  a == b
  • assertArrayEquals(a, b) - compares two arrays
  • assertThat(o, condition) - asserts that o satisfies the condition specified by an instance of org.hamcrest.Matcher