Skip to main content

Nested tests

JUnit Jupiter supports nested test classes to better organize related tests and share setup code. However, nested classes without the @Nested annotation are silently ignored, which can lead to tests not running.

Missing @Nested annotation

Inner classes in JUnit Jupiter test classes are not automatically recognized as test classes. They must be annotated with @Nested or the tests inside them will be silently skipped.

JUnitJupiterTest.java
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

class JUnitJupiterTest {

@Test
void outerTest() {
assertTrue(true);
}

// Missing @Nested annotation
class InnerClass {
@Test
void innerTest() {
assertTrue(true);
}
}
}
danger

The innerTest() method will not run because the InnerClass is missing the @Nested annotation. JUnit silently ignores the inner class, which can give false confidence that tests are passing.

Nested test classes help organize related test scenarios and provide better test report structure.

CalculatorTest.java
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CalculatorTest {

@Test
void addPositiveNumbers() {
assertEquals(5, new Calculator().add(2, 3));
}

@Test
void addNegativeNumbers() {
assertEquals(-5, new Calculator().add(-2, -3));
}

@Test
void subtractPositiveNumbers() {
assertEquals(1, new Calculator().subtract(3, 2));
}

@Test
void subtractNegativeNumbers() {
assertEquals(-1, new Calculator().subtract(-3, -2));
}
}
info

Flat test structure can be hard to navigate as the number of tests grows.