0
0
JUnittesting~10 mins

Why assumptions control test execution in JUnit - Test Your Understanding

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

Complete the code to skip the test if the assumption fails.

JUnit
import static org.junit.jupiter.api.Assumptions.*;

@Test
void testOnlyOnWindows() {
    assumeTrue(System.getProperty("os.name").startsWith([1]));
    // test code here
}
Drag options to blanks, or click blank then click option'
A"Mac"
B"Linux"
C"Windows"
D"Unix"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong OS name string causes the test to skip unexpectedly.
Not using quotes around the OS name string.
2fill in blank
medium

Complete the code to skip the test if the environment variable is not set to 'test'.

JUnit
import static org.junit.jupiter.api.Assumptions.*;

@Test
void testOnlyInTestEnv() {
    assumeTrue("test".equals(System.getenv([1])));
    // test code here
}
Drag options to blanks, or click blank then click option'
A"TEST_ENV"
B"ENVIRONMENT"
C"ENV"
D"APP_ENV"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong environment variable name causes the test to run or skip incorrectly.
Forgetting to use equals() for string comparison.
3fill in blank
hard

Fix the error in the assumption to correctly skip the test when the condition is false.

JUnit
import static org.junit.jupiter.api.Assumptions.*;

@Test
void testSkipIfNotDebug() {
    assumeTrue(System.getProperty("debug") [1] "true");
    // test code here
}
Drag options to blanks, or click blank then click option'
A==
B.equals(
Cequals
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' for string comparison causes the assumption to fail unexpectedly.
Forgetting to close the equals method call with a parenthesis.
4fill in blank
hard

Fill both blanks to assume the test runs only if the Java version is 17 or higher.

JUnit
import static org.junit.jupiter.api.Assumptions.*;

@Test
void testOnlyOnJava17OrHigher() {
    String version = System.getProperty("java.version");
    assumeTrue(Integer.parseInt(version.split("\\.")[[1]]) [2] 17);
    // test code here
}
Drag options to blanks, or click blank then click option'
A0
B1
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 causes wrong version parsing.
Using '<' instead of '>=' causes the test to skip incorrectly.
5fill in blank
hard

Fill all three blanks to assume the test runs only if the system property 'env' is 'dev' and the user is 'admin'.

JUnit
import static org.junit.jupiter.api.Assumptions.*;

@Test
void testOnlyForDevAdmin() {
    assumeTrue(System.getProperty([1]).equals([2]) && System.getProperty([3]).equals("admin"));
    // test code here
}
Drag options to blanks, or click blank then click option'
A"env"
B"dev"
C"user"
D"admin"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up property names causes the assumption to fail.
Using incorrect string literals without quotes.