0
0
JunitHow-ToBeginner ยท 4 min read

How to Use Condition in JUnit Test: Syntax and Examples

In JUnit 5, you can use conditional test execution annotations like @EnabledIf, @DisabledIf, or built-in annotations such as @EnabledOnOs to run tests only when specific conditions are true. Alternatively, use Assumptions.assumeTrue() inside a test method to skip tests dynamically based on conditions.
๐Ÿ“

Syntax

JUnit 5 provides annotations and methods to conditionally run tests:

  • @EnabledIf(expression = "condition"): Runs test if condition is true.
  • @DisabledIf(expression = "condition"): Skips test if condition is true.
  • @EnabledOnOs(OS.WINDOWS): Runs test only on Windows OS.
  • Assumptions.assumeTrue(condition): Skips test at runtime if condition is false.

These help control test execution based on environment or logic.

java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.Assumptions;

public class ConditionalTest {

    @Test
    @EnabledOnOs(OS.WINDOWS)
    void runOnlyOnWindows() {
        // test code here
    }

    @Test
    void runIfConditionTrue() {
        Assumptions.assumeTrue(System.getProperty("user.name").equals("admin"));
        // test code runs only if user is 'admin'
    }
}
๐Ÿ’ป

Example

This example shows a test that runs only on Linux and another test that runs only if a system property matches a value using assumptions.

java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.Assumptions;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class ConditionalExampleTest {

    @Test
    @EnabledOnOs(OS.LINUX)
    void testRunsOnlyOnLinux() {
        assertTrue(System.getProperty("os.name").toLowerCase().contains("linux"));
    }

    @Test
    void testRunsIfPropertySet() {
        Assumptions.assumeTrue("testuser".equals(System.getProperty("user.name")));
        assertTrue(true, "This test runs only if user.name is 'testuser'");
    }
}
Output
Test run summary: - testRunsOnlyOnLinux: executed only on Linux OS - testRunsIfPropertySet: skipped if user.name is not 'testuser'
โš ๏ธ

Common Pitfalls

Common mistakes when using conditions in JUnit tests include:

  • Using assumptions incorrectly, causing tests to be skipped silently without notice.
  • Relying on deprecated annotations from JUnit 4 instead of JUnit 5 conditional annotations.
  • Not setting required system properties or environment variables, leading to unexpected test skips.
  • Using complex expressions in @EnabledIf without proper syntax.
java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assumptions;

public class PitfallTest {

    @Test
    void wrongAssumptionUsage() {
        // This will silently skip test if condition is false
        Assumptions.assumeTrue(false);
        // Test code here will not run, but test is marked as skipped, not failed
    }

    @Test
    void correctAssumptionUsage() {
        Assumptions.assumeTrue(true);
        // Test code runs normally
    }
}
Output
Test run summary: - wrongAssumptionUsage: skipped - correctAssumptionUsage: passed
๐Ÿ“Š

Quick Reference

Use this quick reference to choose the right conditional approach in JUnit 5:

Annotation/MethodPurposeExample Usage
@EnabledOnOs(OS.WINDOWS)Run test only on Windows OS@EnabledOnOs(OS.WINDOWS)
@DisabledOnOs(OS.MAC)Skip test on Mac OS@DisabledOnOs(OS.MAC)
Assumptions.assumeTrue(condition)Skip test at runtime if condition falseAssumptions.assumeTrue(user.equals("admin"))
@EnabledIf(expression = "${sys.user} == 'admin'")Run test if expression true@EnabledIf(expression = "${sys.user} == 'admin'")
โœ…

Key Takeaways

Use JUnit 5 conditional annotations like @EnabledOnOs to run tests based on environment.
Use Assumptions.assumeTrue inside tests to skip tests dynamically when conditions are not met.
Avoid silent test skips by understanding how assumptions work and checking test reports.
Set required system properties or environment variables before running conditional tests.
Prefer JUnit 5 conditional annotations over legacy JUnit 4 approaches for better clarity.