The @Nested tests give the test developer more capabilities to express the relationship among several groups of tests.
Only non-static nested classes (i.e. inner classes) can serve as @Nested test classes.
Nesting can be arbitrarily deep and these inner classes are considered to be full members of the test class family with one exception: @BeforeAll and @AfterAll methods do not work by default. The main reason is that Java does not allow static members in inner classes.
However, this restriction can be circumvented by annotating a @Nested test class with @TestInstance(Lifecycle.PER_CLASS)
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>NestedTestsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
OuterTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.kkjavatutorials; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; public class OuterTest { @BeforeAll static void setUp() { System.out.println("Outer class Test data set up is done.."); } @AfterAll static void tearDown() { System.out.println("Outer class Test data teardown is done.."); } @Test void outerTest1() { System.out.println("outerTest1"); } @Test void outerTest2() { System.out.println("outerTest2"); } @Nested @TestInstance(Lifecycle.PER_CLASS) class innerTest{ @BeforeAll void setUp() { System.out.println("Inner class Test data set up is done.."); } @AfterAll void tearDown() { System.out.println("Inner class Test data teardown is done.."); } @Test void innerTest1() { System.out.println("innerTest1"); } @Test void innerTest2() { System.out.println("innerTest2"); } @Test void innerTest3() { System.out.println("innerTest3"); } } } |
TestingAStack.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.EmptyStackException; import java.util.Stack; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; /** * Nested Tests JUnit 5 Example * @author KK JavaTutorials */ @DisplayName("A Stack") class TestingAStack { private Stack<String> stack; @Test @DisplayName("is instantiated with new Stack()") void isInstantiatedWithNew() { stack = new Stack<String>(); } @Nested @DisplayName("when new") class WhenNew { @BeforeEach void createNewStack() { stack = new Stack<>(); } @Test @DisplayName("is empty") void isEmpty() { assertTrue(stack.isEmpty()); } @Test @DisplayName("throws EmptyStackException when popped") void throwsExceptionWhenPopped() { assertThrows(EmptyStackException.class, stack::pop); } @Test @DisplayName("throws EmptyStackException when peeked") void throwsExceptionWhenPeeked() { assertThrows(EmptyStackException.class, stack::peek); } @Nested @DisplayName("after pushing an element") class AfterPushing { String element = "KK JavaTutorials"; @BeforeEach void pushAnElement() { stack.push(element); } @Test @DisplayName("it is no longer empty") void isNotEmpty() { assertFalse(stack.isEmpty()); } @Test @DisplayName("returns the element when popped and is empty") void returnElementWhenPopped() { assertEquals(element, stack.pop()); assertTrue(stack.isEmpty()); } @Test @DisplayName("returns the peek element but remains not empty") void returnElementWhenPeeked() { assertEquals(element, stack.peek()); assertFalse(stack.isEmpty()); } } } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
That’s all about JUnit 5 Nested Tests Example
If you have any feedback or suggestion please feel free to drop in below comment box.