DEV Community

Cover image for JUnit 5 - Repeated & Conditional Tests
Chathumi Kumarapeli
Chathumi Kumarapeli

Posted on

JUnit 5 - Repeated & Conditional Tests

In this tutorial we are going to learn to write more complex tests using JUnit. 🙂

Repeatable Tests

When you are writing tests there may be instances where you need to run a specific test multiple times. For that JUnit has provided us with the annotation @RepeatedTest. Let's see how we can use it.

import org.junit.jupiter.api.*;
public class TestManageEmployees {
@DisplayName("Checks the Repeated Tests")
    @RepeatedTest(value = 5, name = "Repeating {currentRepetition} of {totalRepetitions}")
    public void TestRepeatedTest() {
        System.out.println("This is a repetition test");
    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see we have given two arguments to @RepeatedTest,

  • value: this indicates the number of times that you want to run the test.
  • name: this is the name that you can give to identify each test per each iteration. >currentRepetition and totalRepetitions are keywords use byJUnit.

When you run the test you get the following output;

8_testRepeatedTest

On left of the terminal you can see the unique name of each iteration and on right you can see the print statement that we have given has printed 5 times as we expected. Yes, I know it is a bit silly example for a test but as far as you get the concept I'm happy with that 😉

Conditional Executions

You may have instances where you want to run tests conditionally. For example, you might want a specific test case to run on a specific operating system. For that JUnit has provided us with two annotations,

  • @EnabledOnOs
  • @DisabledOnOs

Let's see their usage.

@EnabledOnOs

The annotation @EnabledOnOs is used when you want to run a test method on a specific operating system. Check the following code.

import org.junit.jupiter.api.condition.EnabledOnOs;
public class TestManageEmployees {
    @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");
    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see @EnabledOnOs takes in two arguments.

  • value: the OS where you want to RUN the test method.
  • disabledReason: this is the line that displays in the terminal of other operating systems where the test method does not run.

As I'm using Windows this test does not run on my machine. Hence my output looks like this,
6_testEnabledOnOs

@DisabledOnOs

The annotation @DisabledOnOs is the opposite. It is used when you have a test method that you do not want to run on a specific operating system.

import org.junit.jupiter.api.condition.DisabledOnOs;
public class TestManageEmployees {
    @Test
    @DisplayName("Should Be Disabled On Windows")
    @DisabledOnOs(value = OS.WINDOWS, disabledReason = "Test is disabled on Windows")
    public void TestDisabledOnOS() {
        System.out.println("Tests DisabledOnOs annotation");
    }
}
Enter fullscreen mode Exit fullscreen mode

@DisabledOnOs annotation also takes the same two arguments,

  • value: the operating system on which you DO NOT want to run the test.
  • disabledReason: this is the line that displays in the terminal of other operating systems that you have specified in the value section. The above test gave me the output,

7_testDisabledOnOS

With that we can wrap up the tutorial on repeatable and conditional tests. Want to know how to pass parameters into test methods? Check the next tutorial - JUnit 5 - Parameterized Tests 😋

Top comments (0)