JUnit
JUnit is the standard testing library for Java. It gives you annotations to mark test methods, assertions to check results, and a runner that build tools and IDEs know how to launch.
Detailed Explanation
A unit test is a small program that calls your code with known inputs and fails loudly when the answer is wrong. JUnit is the library that makes those programs easy to write and easy to run. You mark a method with @Test, call the code, and assert what you expect.
Before JUnit existed, people checked behaviour by adding a main method that printed values, then reading the output by eye. That works for one class and collapses for a thousand. Nobody re-reads the output of yesterday's checks. JUnit replaced human reading with a pass/fail verdict, which means the checks can run on every commit without anyone watching.
The current version is JUnit 5, and it is three pieces rather than one jar. Knowing the split matters because build errors usually name one of them.
- JUnit Platform is the launcher. Maven Surefire, Gradle, and your IDE talk to the platform, not to your tests directly.
- JUnit Jupiter is the part you write against: the
org.junit.jupiter.apiannotations and assertions. - JUnit Vintage is an engine that runs old JUnit 4 tests on the same platform, so a team can migrate gradually instead of in one weekend.
In practice you add one dependency, org.junit.jupiter:junit-jupiter, and the platform arrives with it. Your build tool discovers test classes by file-name convention, usually anything ending in Test.
The behaviour that surprises people first is instance lifecycle. JUnit creates a new instance of the test class for every test method. A field you set in one test is gone in the next. That is deliberate: it stops tests from depending on each other and on their running order. The exception is @BeforeAll and @AfterAll methods, which run once per class and therefore must be static unless you switch the class to @TestInstance(Lifecycle.PER_CLASS).
Reach for JUnit any time a piece of code has a rule you can state in a sentence. Discount bands, date parsing, input validation, and money rounding all qualify. If you cannot state the rule, write the test first and the rule usually becomes obvious while you type.
The cost is build time. Every test runs on every build, so a suite that touches the network or a real database turns a thirty-second build into a five-minute one, and a five-minute build is a build people skip. Keep unit tests in memory and push the slow checks into a separate integration suite.
Code Examples
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
// Under JUnit 5 neither the class nor the test method needs to be public.
// JUnit 4 required both, which is why old test code is full of "public void".
class CartTest {
// The class under test. In a real project this lives in src/main/java.
static class Cart {
private int totalCents;
void add(int priceCents, int quantity) {
totalCents += priceCents * quantity;
}
int totalCents() {
return totalCents;
}
}
@Test
@DisplayName("two line items add up to the cart total")
void addsLineItemsToTheTotal() {
var cart = new Cart();
cart.add(499, 2); // two coffees at 4.99 each
cart.add(1250, 1); // one mug at 12.50
// Expected value first, actual value second.
// JUnit 4 took the optional message first; JUnit 5 takes it last.
assertEquals(2248, cart.totalCents());
}
}Common Gotchas
The JUnit 4 import silently does nothing
a method annotated with `org.junit.Test` is invisible to the Jupiter engine. The class compiles, the build passes, and the test never runs. Check the import line is `org.junit.jupiter.api.Test` before you trust a green build.
A test with no assertion always passes
calling a method and never checking the result produces a green tick that means only "nothing threw". Every test needs at least one assertion or an explicit `assertThrows`.
Test order is deliberately unspecified
JUnit does not run methods top to bottom, and the order can change between versions. If two tests only pass in sequence, they share state that should have been reset.