#JUnit compared with Jest
AddedJUnit is Java's test framework, and its current generation is called JUnit Jupiter. Spring Boot 4.2 ships JUnit 6.1.3: version 6 (September 2025) raised the minimum to Java 17 and gave the platform and the Jupiter engine one shared version number, but the API you write against, org.junit.jupiter.api, is the same one JUnit 5 had. If you last used JUnit 4 (@RunWith, @Before, public test methods), that's the part that changed: no runners, lifecycle annotations renamed, test classes and methods can be package-private.
The mental model maps almost one to one onto Jest. A test class is a describe block, each @Test method is an it, and @Nested inner classes are nested describes. JUnit creates a new instance of the test class for every test method, so instance fields are fresh each time: that's why a field set in @BeforeEach is safe, and why state can't leak between tests through fields.
Assertions come from a separate library. JUnit has its own (assertEquals(expected, actual)), but Spring's starter includes AssertJ, whose fluent assertThat(actual).isEqualTo(expected) reads like Jest's expect(actual).toBe(expected) and gives far better failure messages. Use AssertJ.
| Jest | JUnit Jupiter + AssertJ |
|---|---|
describe("Cart", …) | a test class CartTest, @DisplayName("Cart") |
nested describe | a @Nested inner class |
it("…", …) / test(…) | a @Test method (name it like a sentence) |
beforeEach / afterEach | @BeforeEach / @AfterEach |
beforeAll / afterAll | @BeforeAll / @AfterAll (static methods) |
expect(x).toBe(y) / toEqual(y) | assertThat(x).isEqualTo(y) (uses equals) |
expect(list).toEqual([a, b]) | assertThat(list).containsExactly(a, b) |
expect(fn).toThrow("msg") | assertThatThrownBy(() -> …).hasMessageContaining("msg") |
it.skip / it.only | @Disabled / run one test: ./mvnw test -Dtest='CartTest#name' |
it.each | @ParameterizedTest (next topic) |
jest.fn() / jest.mock() | Mockito's mock() / Spring's @MockitoBean (below) |
package com.example.shop;
import java.util.ArrayList;
import java.util.List;
public class Cart {
public record Line(String item, long priceCents, int quantity) {
}
private final List<Line> lines = new ArrayList<>();
public void add(String item, long priceCents, int quantity) {
if (quantity < 1) {
throw new IllegalArgumentException("quantity must be at least 1, was " + quantity);
}
lines.add(new Line(item, priceCents, quantity));
}
public List<String> items() {
return lines.stream().map(Line::item).toList();
}
public long totalCents() {
return lines.stream().mapToLong(l -> l.priceCents() * l.quantity()).sum();
}
}package com.example.shop;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@DisplayName("Cart")
class CartTest {
private Cart cart;
@BeforeEach
void newCart() {
cart = new Cart();
}
@Test
@DisplayName("is empty when created")
void emptyAtFirst() {
assertThat(cart.items()).isEmpty();
assertThat(cart.totalCents()).isZero();
}
@Nested
@DisplayName("with two lines")
class WithTwoLines {
@BeforeEach
void addLines() {
cart.add("Keyboard", 4999, 1);
cart.add("Mouse", 1500, 2);
}
@Test
void listsItemsInOrder() {
assertThat(cart.items()).containsExactly("Keyboard", "Mouse");
}
@Test
void totalsPriceTimesQuantity() {
assertThat(cart.totalCents()).isEqualTo(7999);
}
}
@Test
void rejectsZeroQuantity() {
assertThatThrownBy(() -> cart.add("Keyboard", 4999, 0))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("at least 1");
}
}$ ./mvnw test
[INFO] Results:
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESSCartTestis plain Java: no Spring, no annotations on the class except a display name. Maven's test plugin (Surefire) finds it because its name ends inTestand it's undersrc/test/java.- Before every test, JUnit creates a new
CartTestobject and callsnewCart(), so each test starts with an empty cart.emptyAtFirstchecksitems()is empty and the totalisZero(). WithTwoLinesis a@Nestedclass: an inner class, so it can see the outercartfield. For its tests JUnit runs the outer@BeforeEachfirst, then the inner one, exactly like nestedbeforeEachin Jest. Both tests start with a keyboard and two mice.containsExactly("Keyboard", "Mouse")checks contents and order;isEqualTo(7999)checks 4999 × 1 + 1500 × 2.assertThatThrownByruns the lambda, expects it to throw, and then asserts on the exception's type and message.- The summary:
Tests run: 5, Failures: 0. That's the four tests inCartTest(two at the top level, two nested) plus the starter'scontextLoads.
Here the cart has a bug (it forgets the quantity) and one test expects the wrong order. The summary names each failure and AssertJ explains it:
package com.example.shop;
import java.util.ArrayList;
import java.util.List;
public class Cart {
public record Line(String item, long priceCents, int quantity) {
}
private final List<Line> lines = new ArrayList<>();
public void add(String item, long priceCents, int quantity) {
lines.add(new Line(item, priceCents, quantity));
}
public List<String> items() {
return lines.stream().map(Line::item).toList();
}
public long totalCents() {
return lines.stream().mapToLong(Line::priceCents).sum();
}
}package com.example.shop;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class CartTest {
private Cart cart;
@BeforeEach
void addLines() {
cart = new Cart();
cart.add("Keyboard", 4999, 1);
cart.add("Mouse", 1500, 2);
}
@Test
void listsItemsInOrder() {
assertThat(cart.items()).containsExactly("Mouse", "Keyboard");
}
@Test
void totalsPriceTimesQuantity() {
assertThat(cart.totalCents()).isEqualTo(7999);
}
}$ ./mvnw test
[INFO] Results:
[ERROR] Failures:
[ERROR] CartTest.listsItemsInOrder:20
Expecting actual:
["Keyboard", "Mouse"]
to contain exactly (and in same order):
["Mouse", "Keyboard"]
but there were differences at these indexes:
- element at index 0: expected "Mouse" but was "Keyboard"
- element at index 1: expected "Keyboard" but was "Mouse"
[ERROR] CartTest.totalsPriceTimesQuantity:25
expected: 7999L
but was: 6499L
[ERROR] Tests run: 3, Failures: 2, Errors: 0, Skipped: 0
[ERROR] BUILD FAILURElistsItemsInOrderexpects["Mouse", "Keyboard"]but the cart keeps insertion order. AssertJ prints the actual list, the expected one, and each index that differs: much more than Jest's diff for a two-element array, and the same idea.totalsPriceTimesQuantityfails because the buggytotalCentsignores the quantity:expected: 7999L but was: 6499L(4999 + 1500). TheLsays the values were compared aslong.- Each failure is named by class, method and the line of the failing assertion:
CartTest.listsItemsInOrder:20andCartTest.totalsPriceTimesQuantity:25, which is enough to jump straight to it. Tests run: 3, Failures: 2andBUILD FAILURE. A failing test stops the Maven build at thetestphase, so./mvnw packagewould produce no JAR (chapter 11).