Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Operating System Conditions
3.
Java Runtime Environment Conditions
4.
System Property Conditions
5.
Environment Variable Conditions
6.
Frequently Asked Questions
6.1.
What are Operating System Conditions?
6.2.
What are Java Runtime Environment Conditions?
6.3.
What are System Property Conditions?
6.4.
What are Environment Variable Conditions?
6.5.
How do you ignore test cases in JUnit?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Conditional tests

Author Parth Jain
1 upvote
gp-icon
Basics of javascript
Free guided path
10 chapters
68+ problems
gp-badge
Earn badges and level up

Introduction

In this article, we're going to discuss the various conditional test available in JUnit along with their execution.
There are conditional annotations available in the JUnit library condition packages that allow us to provide specific conditions on our tests.

Operating System Conditions

There are cases where we might need to alter our test scenarios depending on the operating system. To do so, the annotation @EnabledOnOs is extremely useful.
To use this annotation, we need to provide a value that states the type of Operating System. The value is not limited to a single Operating system and can accept an array of multiple operating systems.
To understand the annotation clearly, here is an example.
For example, let's say we want to enable a test to run only on Windows and macOS:

@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX})
public void willRunBothWindowsAndLinux() {
    //...
}
Opposite to the @EnabledOnOs, we have @DisabledOnOs, which, as the name implies, disables tests as per the operating system stated as an argument.
@Test
@DisabledOnOs(OS.WINDOWS)
public void willNotRunAtWindows() {
    //...
}

Here we have a complete code snippet that shows how both @EnablesOnOs and @DisabledOnOs annotations are used.

package com.mkyong.conditional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

public class OperatingSystemTest {

    @Test
    @EnabledOnOs({OS.LINUX, OS.MAC})
    void onLinuxOrMac() {
        System.out.println("This will run on Linux or Mac OS Only!");
    }

    @Test
    @EnabledOnOs({OS.WINDOWS})
    void onWindows() {
        System.out.println("This will run on Windows only!");
    }

    @Test
    @DisabledOnOs(OS.LINUX)
    void notOnLinux() {
        System.out.println("This won’t run on Linux");
    }

    @Test
    @DisabledOnOs({OS.LINUX, OS.AIX, OS.SOLARIS, OS.MAC})
    void notOnLinuxOrAixOrSolarisOrMac() {
        System.out.println("This will not run on Linux, AIX, Solaris or MAC!");
    }
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Java Runtime Environment Conditions

There are cases where we might need to run our test cases only on a specific version of JRE. This can be done using the annotations @EnableOnJre and @DisableOnJre.Similar to the EnablesOS annotation. These can accept multiple values as arguments.

@Test
@EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11})
public void willOnlyRunOnJava8And11() {
    //...
}

On the contrary, if we wish to disable our tests running with a particular version of JRE, we can accomplish it using the following code.

@Test
@DisabledOnJre(JRE.JAVA_10)
public void thisTestWillNotRunonJRE10() {
    // this test will not run on Java 10.
}

Finally, here is a complete code snippet. 

package com.mkyong.conditional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;

public class JreTest {

    @Test
    @EnabledOnJre(JRE.JAVA_10)
    void onJava10() {
        System.out.println("This will run on Java version 10");
    }

    @Test
    @EnabledOnJre({JRE.JAVA_10, JRE.JAVA_13})
    void onJava10OrJava13() {
        System.out.println("This will run only on Java version 10 or Java 13");
    }
    @Test
    @DisabledOnJre(JRE.JAVA_8)
    void notOnJava9() {
        System.out.println("This will not run on Java version 8");
    }
}

System Property Conditions

There might be a case where we want our test cases to run based on some system properties of the JVM. This action can be performed with the help of @EnabledIfSystemProperty and @DisabledIfSystemProperty annotations.
To use these annotations, we must use two arguments called named and matches arguments. The named argument helps specify the exact system property and the matches argument helps define the pattern of property value with an expression.
To understand, consider the following code snippet.

package com.mkyong.conditional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import java.util.Properties;

public class SystemPropertyTest {

    @Test
    @EnabledIfSystemProperty(named = "java.vm.name", matches = ".*OpenJDK.*")
    void RunOnOpenJDK() {
        System.out.println("Run this on OpenJDK!");
    }

@Test
@DisabledIfSystemProperty(named = "os.arch", matches = ".*64.*")
void NotOn64BitArchitectures() {
   System.out.println("Will not run on a 64bit architecture");
}
@Test
@DisabledIfSystemProperty(named = "ci-server", matches = "true")
void notOnCiServer() {
    // ...
}
    @Test
    @DisabledIfSystemProperty(named = "user.country", matches = "IN")
    void notOnCountryIndia() {
        System.out.println("Will not run for indian users");
    }
}

Environment Variable Conditions

There may be cases where we need to specify test conditions based on the environment variable conditions. This action can be accomplished with the help of @EnabledIfEnvironmentVariable and @DisabledIfEnvironmentVariable annotations. Like System property annotations, the Environment variable annotations accept two arguments named and matches for providing the environment variable name and expression.

@Test
@EnabledIfEnvironmentVariable(named = "GDMSESSION", matches = "ubuntu")
public void willRunOnUbuntuServer() {
    //...
}
@Test
@DisabledIfEnvironmentVariable(named = "LC_TIME", matches = ".*UTF-8.")
public void willNotRunWhenTimeIsNotUTF8() {
    //...
}

A complete test snippet is as follows.

package com.mkyong.conditional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.util.Map;
public class EnvVariableTest {

    @Test
    @EnabledIfEnvironmentVariable(named = "PROCESSOR_IDENTIFIER", matches = ".*Intel64 Family 5.*")
    void onIntel64() {
        System.out.println("Will run only on Intel6 Family 5");
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "NUMBER_OF_PROCESSORS", matches = "4")
    void onProcessor8() {
        System.out.println("Will run only if it has four processors.");
    }
}

Frequently Asked Questions

What are Operating System Conditions?

There are cases where we might need to alter our test scenarios depending on the operating system. To do so, the annotation @EnabledOnOs is extremely useful.

What are Java Runtime Environment Conditions?

There are cases where we might need to run our test cases only on a specific version of JRE. This can be done by using the annotations @EnableOnJre and @DisableOnJre.

What are System Property Conditions?

There might be a case where we want our test cases to run based on some system properties of the JVM. This can be performed with the help of @EnabledIfSystemProperty and @DisabledIfSystemProperty annotations.

What are Environment Variable Conditions?

In cases where we need to specify test conditions based on the environment variable conditions. This can be accomplished with the help of @EnabledIfEnvironmentVariable and @DisabledIfEnvironmentVariable annotations.

How do you ignore test cases in JUnit?

To ignore a test case in JUnit, we can use the @Ignore annotation. This annotation can be applied to a test method in order to skip the test execution. 

Conclusion

In this article, we have extensively discussed the various types of Conditional tests in JUnit and how they can be implemented. We hope that this blog has helped you enhance your knowledge of JUnit and if you want to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding!"

Previous article
Dynamic Tests in Junit 5
Next article
JUnit5 Dependency Injection for constructors and Methods
Guided path
Free
gridgp-icon
Basics of javascript
10 chapters
68+ Problems
gp-badge
Earn badges and level up
Live masterclass