Definition. Unit testing with JUnit

[comp1110-40]

A unit test checks a single, small piece of a program in isolation from the rest of the system. Unit tests contrast with integration tests, which test how multiple components work together.

JUnit is the standard unit-testing framework for Java. A JUnit test is an ordinary Java method annotated with @Test. Inside the method, special JUnit methods are used similarly to the assertions were used previously in this course:

  • assertTrue(expr) — fails if expr is false
  • assertFalse(expr) — fails if expr is true
  • assertEquals(expected, actual) — fails if the two values differ
  • Many more, see the JUnit documentation for a full list.

If any assertion fails, JUnit reports the test as failed and shows which assertion was violated.

bars magnifying-glass caret-down plus minus arrow-right xmark