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!"