JUnit 5 Conditional Test Examples
This article shows you how to use JUnit 5 to enable or disable tests based on conditions.
P.S Tested with JUnit 5.5.2
1. Operating System
1.1 Enabled or disabled tests based on a particular operating system via @EnabledOnOs
and @DisabledOnOs
annotations.
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.WINDOWS})
void onLinuxOrWindows() {
System.out.println("Run this on Linux or Windows!");
}
@Test
@EnabledOnOs({OS.WINDOWS})
void onWindows() {
System.out.println("Run this on Windows!");
}
@Test
@DisabledOnOs(OS.WINDOWS)
void notOnWindows() {
System.out.println("Do not run this on Windows!");
}
@Test
@DisabledOnOs({OS.WINDOWS, OS.AIX, OS.SOLARIS, OS.MAC})
void notOnWindowsOrAixOrSolarisOrMac() {
System.out.println("Do not run this on Windows, AIX, Solaris or MAC!");
}
}
Output – Test with Windows OS.
2. Java Runtime Environment
2.1 Enabled or disabled tests based on a particular Java Runtime Environment (JRE) via @EnabledOnJre
and @DisabledOnJre
annotations.
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_9)
void onJava9() {
System.out.println("Run this on Java 9");
}
@Test
@EnabledOnJre({JRE.JAVA_12, JRE.JAVA_13})
void onJava12OrJava13() {
System.out.println("Run this on Java 12 or Java 13");
}
@Test
@DisabledOnJre(JRE.JAVA_9)
void notOnJava9() {
System.out.println("Do not run this on Java 9");
}
}
Output – Test with Java 13
3. System Property
3.1 Enabled or disabled tests based on the value of the named
from the JVM system property via @EnabledIfSystemProperty
and @DisabledIfSystemProperty
annotations.
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 onOpenJDK() {
System.out.println("Run this on OpenJDK!");
}
@Test
@DisabledIfSystemProperty(named = "user.country", matches = "MY")
void notOnCountryMalaysia() {
System.out.println("Do not run this on country code MY");
}
@Test
void printSystemProperties() {
Properties properties = System.getProperties();
properties.forEach((k, v) -> System.out.println(k + ":" + v));
}
}
Read this – How to print all System properties in Java
4. Environment Variable
4.1 Enabled or disabled tests based on the value of the named
from the environment property via @EnabledIfEnvironmentVariable
and @DisabledIfEnvironmentVariable
annotations.
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 6.*")
void onIntel64() {
System.out.println("Run this on Intel6 Family 6 only.");
}
@Test
@EnabledIfEnvironmentVariable(named = "NUMBER_OF_PROCESSORS", matches = "8")
void onProcessor8() {
System.out.println("Run this if it has 8 processors.");
}
@Test
@DisabledIfEnvironmentVariable(named = "CURRENT_ENV", matches = ".*development.*")
void notOnDeveloperPC() {
System.out.println("Do not run this if env variables 'CURRENT_ENV' matches .*development.* ");
}
@Test
void printEnvironmentProperties() {
Map<String, String> env = System.getenv();
env.forEach((k, v) -> System.out.println(k + ":" + v));
}
}
Download Source Code
$ cd junit5-examples
$ check src/test/java/com/mkyong/conditional/*.java
I need to disable certain tests on Windows and I tried your example. The annotation @DisabledOnOs(OS.WINDOWS) is not disabled in Windows. It ran and failed even it is disabled. It will be great if you can provide me with a better solution. Thanks in advance.