Example of using nested tests in Junit5.
Reference: https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested
Nested tests give the test writer more capabilities to express the relationship among several group of tests.
- Only non-static nested classes (i.e. inner classes) can
serve as
@Nested
test classes. - Nesting can be arbitrarily deep
@BeforeAll
and@AfterAll
methods cannot be defined in an@Nested
class - Java does not allow static members in inner classes.
It is a good practice to group tests of one method in an inner class:
- suppose we want to test:
class MathGroup { OptionalDouble average(Collection<Integer> ints) { if (isNull(ints)) { throw new IllegalArgumentException("collection cannot be null"); } return ints.stream() .filter(Objects::nonNull) .mapToInt(x -> x) .average(); } }
- method
average(Collection<Integer> ints)
- throws
IllegalArgumentException
if input is null - ignore nulls in non null collection
- return
OptionalDouble
- throws
- we group tests in the inner class
class NestedTests { // tests of other methods... @Nested @DisplayName("tests of average(Collection<Integer> ints)") class Average_collection { MathGroup mathGroup = new MathGroup(); @Test void nullCollection() { assertThrows(IllegalArgumentException.class, () -> mathGroup.average(null)); } @Test void emptyCollection() { assertTrue(mathGroup.average(Collections.emptyList()).isEmpty()); } @Test @DisplayName("null elements are ignored") void collectionWithNullElement() { assertThat(mathGroup.average(Arrays.asList(1, null)), is(OptionalDouble.of(1))); } @Test void wellFormattedInput() { assertThat(mathGroup.average(Arrays.asList(1, 2, 3, 11)), is(OptionalDouble.of(4.25))); } } // tests of other methods... }
- reports are well formatted:
Test Results NestedTests tests of average(Collection<Integer> ints) wellFormattedInput() collectionWithNullElement() emptyCollection() nullCollection()