Bird
Raised Fist0
JUnittesting~10 mins

Custom conditions with @EnabledIf in JUnit - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable the test only if the system property 'env' equals 'test'.

JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

public class EnvTest {
    @Test
    @EnabledIf(expression = "${env} == '[1]'", reason = "Only runs in test environment")
    void runOnlyInTestEnv() {
        System.out.println("Test running in test environment");
    }
}
Drag options to blanks, or click blank then click option'
Atest
Bprod
Cdev
Dstage
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong environment string like 'prod' or 'dev' which disables the test.
Forgetting to put the string in quotes inside the expression.
2fill in blank
medium

Complete the code to enable the test only if the Java version is 17.

JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

public class JavaVersionTest {
    @Test
    @EnabledIf(expression = "${java.version} == '[1]'", reason = "Run only on Java 17")
    void runOnlyOnJava17() {
        System.out.println("Running on Java 17");
    }
}
Drag options to blanks, or click blank then click option'
A11
B8
C17
D19
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different Java version number like '11' or '8'.
Not quoting the version string properly in the expression.
3fill in blank
hard

Fix the error in the expression to enable the test only if the OS name contains 'Windows'.

JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

public class OsTest {
    @Test
    @EnabledIf(expression = "[1].contains('Windows')", reason = "Run only on Windows OS")
    void runOnlyOnWindows() {
        System.out.println("Running on Windows OS");
    }
}
Drag options to blanks, or click blank then click option'
ASystem.os.name
Bos.name
C"os.name"
DSystem.getProperty("os.name")
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'os.name' which is not a valid variable.
Using a string literal '"os.name"' which is not the OS name value.
4fill in blank
hard

Fill both blanks to enable the test only if the environment is 'prod' and the Java version is at least 11.

JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

public class ProdJavaTest {
    @Test
    @EnabledIf(expression = "${env} == '[1]' && Integer.parseInt(${java.version}) [2] 11", reason = "Run only in prod with Java 11 or higher")
    void runInProdJava11Plus() {
        System.out.println("Running in prod with Java 11+");
    }
}
Drag options to blanks, or click blank then click option'
Aprod
B>=
C<=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' for Java version comparison.
Using wrong environment string like 'test' or 'dev'.
5fill in blank
hard

Fill all three blanks to enable the test only if the OS is Linux, the environment is 'dev', and the Java version is exactly 17.

JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

public class ComplexConditionTest {
    @Test
    @EnabledIf(expression = "System.getProperty('os.name').toLowerCase().contains('[1]') && ${env} == '[2]' && ${java.version} == '[3]'", reason = "Run only on Linux dev environment with Java 17")
    void runOnLinuxDevJava17() {
        System.out.println("Running on Linux dev with Java 17");
    }
}
Drag options to blanks, or click blank then click option'
Alinux
Bdev
C17
Dwindows
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'windows' instead of 'linux' for OS check.
Using wrong environment like 'prod' instead of 'dev'.
Using wrong Java version like '11' instead of '17'.

Practice

(1/5)
1. What is the primary purpose of using @EnabledIf in JUnit tests?
easy
A. To run a test only if a custom condition returns true
B. To disable a test permanently
C. To mark a test as expected to fail
D. To repeat a test multiple times

Solution

  1. Step 1: Understand the role of @EnabledIf

    @EnabledIf is used to conditionally enable a test based on a custom boolean expression or method.
  2. Step 2: Compare with other annotations

    Unlike disabling or repeating tests, @EnabledIf controls test execution dynamically based on conditions.
  3. Final Answer:

    To run a test only if a custom condition returns true -> Option A
  4. Quick Check:

    @EnabledIf runs test if condition true [OK]
Hint: Remember: @EnabledIf controls test run by condition [OK]
Common Mistakes:
  • Confusing @EnabledIf with @Disabled
  • Thinking it repeats tests
  • Assuming it marks tests as expected failures
2. Which of the following is the correct syntax to enable a test only if a static method isFeatureEnabled() returns true using @EnabledIf?
easy
A. @EnabledIf("isFeatureEnabled()")
B. @EnabledIf(expression = "isFeatureEnabled()")
C. @EnabledIf("isFeatureEnabled")
D. @EnabledIf(condition = "isFeatureEnabled()")

Solution

  1. Step 1: Check correct attribute name

    The correct attribute for the condition expression is unnamed, so just a string is passed to @EnabledIf.
  2. Step 2: Validate method call syntax

    The method must be called with parentheses inside the string: "isFeatureEnabled()".
  3. Final Answer:

    @EnabledIf("isFeatureEnabled()") -> Option A
  4. Quick Check:

    Use @EnabledIf("method()") syntax [OK]
Hint: Use @EnabledIf("method()") with parentheses [OK]
Common Mistakes:
  • Using expression attribute which does not exist
  • Omitting parentheses in method call
  • Using incorrect attribute name like condition
3. Given the following code snippet, what will be the output when running the test?
public class FeatureTest {
  static boolean isFeatureEnabled() { return false; }

  @Test
  @EnabledIf("isFeatureEnabled()")
  void testFeature() {
    System.out.println("Feature test executed");
  }
}
medium
A. Feature test executed
B. Test skipped, no output
C. Compilation error due to @EnabledIf
D. Runtime exception thrown

Solution

  1. Step 1: Evaluate the condition method

    The method isFeatureEnabled() returns false, so the condition for @EnabledIf is false.
  2. Step 2: Understand @EnabledIf behavior

    When the condition is false, the test is skipped and the test method body does not run.
  3. Final Answer:

    Test skipped, no output -> Option B
  4. Quick Check:

    False condition skips test [OK]
Hint: False condition means test does not run [OK]
Common Mistakes:
  • Expecting test to run despite false condition
  • Thinking it causes errors
  • Confusing with @Disabled
4. Identify the error in the following test code using @EnabledIf:
public class SampleTest {
  @Test
  @EnabledIf("checkCondition")
  void testSomething() {}

  boolean checkCondition() { return true; }
}
medium
A. checkCondition must return String, not boolean
B. @EnabledIf requires expression attribute
C. checkCondition method must be static
D. Method checkCondition must be public

Solution

  1. Step 1: Check method signature requirements

    The method used in @EnabledIf must be static because JUnit calls it without instance.
  2. Step 2: Verify method return type and visibility

    Return type boolean is correct and method visibility can be default or public, but static is mandatory.
  3. Final Answer:

    checkCondition method must be static -> Option C
  4. Quick Check:

    Condition method must be static [OK]
Hint: Condition methods must be static [OK]
Common Mistakes:
  • Making condition method non-static
  • Expecting expression attribute in @EnabledIf
  • Confusing return type requirements
5. You want to enable a test only if the system property env equals prod and a static method isReady() returns true. Which @EnabledIf expression correctly combines these conditions?
hard
A. @EnabledIf(expression = "System.getProperty('env').equals('prod') && isReady()")
B. @EnabledIf("System.getProperty('env') == 'prod' && isReady()")
C. @EnabledIf("System.getProperty('env').equals('prod') & isReady()")
D. @EnabledIf("'prod'.equals(System.getProperty('env')) && isReady()")

Solution

  1. Step 1: Understand string comparison in Java expressions

    Use 'prod'.equals(System.getProperty('env')) to avoid NullPointerException if property is null.
  2. Step 2: Combine conditions with logical AND

    Use && to combine boolean expressions correctly in the string.
  3. Step 3: Validate syntax of @EnabledIf

    The expression is passed as a single string without attribute name; @EnabledIf("'prod'.equals(System.getProperty('env')) && isReady()") matches this.
  4. Final Answer:

    @EnabledIf("'prod'.equals(System.getProperty('env')) && isReady()") -> Option D
  5. Quick Check:

    Use safe equals and && in string expression [OK]
Hint: Use 'prod'.equals(...) and && for safe combined condition [OK]
Common Mistakes:
  • Using == for string comparison
  • Using single & instead of &&
  • Adding expression attribute which is invalid
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard