JUnit Framework

In this course, we use a Java library called JUnit to write and run tests.

JUnit provides annotations to write tests using the familiar structure and syntax of Java classes and methods but treat them as "tests" once annotated as one.

The library is included in the lib folder which we provide to you as the starter/solution for homework and chapters.

Let's create a test class for the IndexedList ADT:

import org.junit.jupiter.api.Test;

class IndexedListTest {

  @Test
  void put() {
  }

  @Test
  void get() {
  }

  @Test
  void length() {
  }
}

You must store the IndexedListTest.java file inside the folder src/test/java.

Conventionally, a test class has the word "Test" in its name either preceding or following the class name the tests are written for.

Each method that is annotated with @Test is a unit test.

A "unit test" is a term used in software engineering to refer to a test written to examine a unit — the smallest piece of code that can be logically isolated in a system. In most programming languages, that unit is a function or a method.

Notice, to use @Test annotation, it must be imported at the top of your source code.

IntelliJ comes equipped with a plugin that facilitates running JUnit tests. You can click on the green play arrow next to each test to run it.

Resources