JUnit

JUnit is an open source test framework for java. It focuses in unit tests and according to Wikipedia, it is the most popular external library for java on github. Kent Beck is one of the developers of the project.

Here is a simple code example to show its syntax:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class CalculatorTest {
  @Test
  public void evaluatesExpression() {
    Calculator calculator = new Calculator();
    int sum = calculator.evaluate("1+2+3");
    assertEquals(6, sum);
  }
}

On a very basic level, JUnit “asserts” or checks that both values sent to the function are equal. You should send the output of a function and its expected output. If everything works, JUnit won’t report errors. If there are errors, JUnit will try to give a detailed explanation of which parts of the input didn’t match.

JUnit has any more tools to make testing easier. In a large project, the amount of tests will be huge so there are many ways to group tests on different classes and methods.

JUnit can be used for Test-Driven Development. This technique consists on firs writing a set of tests that a program should pass and then making code that passes them. This kind of focus means that you have to know what a piece of code will do even before you write it, so you’re required to have  clear idea of the code. Knowing exactly what to do allows you to code only that and not lose time thinking about functionality at the same time as programming.

Here is the official FAQ to answer any specific question.