Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The test should run only when the 'env' system property equals 'test', so the expression must check for 'test'.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The test should run only if the Java version is exactly '17', so the expression must check for '17'.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The expression must call System.getProperty("os.name") to get the OS name string before calling contains().
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' for Java version comparison.
Using wrong environment string like 'test' or 'dev'.
✗ Incorrect
The environment must be 'prod' and the Java version must be greater than or equal to 11 to enable the test.
5fill in blank
hardFill 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'
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'.
✗ Incorrect
The OS name must contain 'linux', environment must be 'dev', and Java version must be '17' to enable the test.