DEV Community

Cover image for JUnit 5 - Nested & Disabled Tests
Chathu Rashmini
Chathu Rashmini

Posted on

JUnit 5 - Nested & Disabled Tests

Nested Tests

Nested Test is used to group several test methods together into a single logical group making the test class looks more organized 😌 The annotation we should use to write Nested Tests is @Nested. The writing of nested tests is a bit different than writing other test methods. Here we declare a class to include all the tests with the annotation @Nested. Check the example given below.

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

public class TestManageEmployees {
    @Nested
    class NestedTestClass {

        @Test
        @DisplayName("Should Be Enabled Only On MAC OS")
        @EnabledOnOs(value = OS.MAC, disabledReason = "Test is only enabled on MAC OS")
        public void TestEnabledOnOS() {
            System.out.println("Tests EnabledOnOs annotation");
        }

        @DisplayName("Add Employee using CsvFileSource")
        @ParameterizedTest
        @CsvFileSource(resources = "/data.csv")
        public void TestParameterizedTestCsvFileSource(String contact_number) {
            ManageEmployees employees = new ManageEmployees();
            employees.addEmployee("Alice", "Cullen", contact_number);
            Assertions.assertFalse(employees.getEmployees().isEmpty());
            Assertions.assertEquals(1, employees.getEmployees().size());
            System.out.println("Line with the contact number: " + contact_number);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above code we have the class NestedTestClass which has @Nested annotation. Inside this class we have declared two test methods. This makes the test class clean and tidy. I am not going to explain those two tests, because I'm pretty sure now you are a pro in understanding such simple test methods 😁

Nested Test classes only allow to use the two annotations @BeforeEach and @AfterEach. Which means you cannot use the annotations @BeforeAll or @AfterAll inside a Nested test class.

Disabled Tests

Lastly we are going to talk about disabled tests. This is used to disable any test from execution. You should use the annotation @Disabled in case you want to disable a test from execution.

@Test
@Disabled
public void TestDisabled() {
    ManageEmployees employees = new ManageEmployees();
    employees.addEmployee("Emmet", "Cullen", "0123458762");
}
Enter fullscreen mode Exit fullscreen mode

No magic in the above code. Just the same structure that you are familiar with. We give the test the annotation @Disabled and the test stays without executing and you can see this output in the terminal;
13_Disabled

Yes, We did it!!! 🥳 Here ends the tutorial series on JUnit 🤩 I'm pretty sure now you are quite familiar on writing test methods.

You can find all the source codes with the test methods that we wrote in this tutorial series, by heading into my git repository. The folder LearnJUnitFinal contains the finalized code set.

Don't stop from here. Continue on practicing 👩‍💻 Happy Testing 😎

Discussion (0)