0
0
JUnittesting~10 mins

Custom conditions with @EnabledIf in JUnit - Interactive Code Practice

Choose your learning style9 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'.