0
0
JUnittesting~10 mins

assumeTrue and assumeFalse 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 skip the test if the condition is false using assumeTrue.

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

@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"Windows"
C"Linux"
D"Unix"
Attempts:
3 left
💡 Hint
Common Mistakes
Using assumeFalse instead of assumeTrue here.
Not using quotes around the string "Windows".
Using incorrect OS name string.
2fill in blank
medium

Complete the code to skip the test if the condition is true using assumeFalse.

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

@Test
void testSkipOnCI() {
    assumeFalse("CI".equals(System.getenv([1])));
    // test code here
}
Drag options to blanks, or click blank then click option'
A"BUILD"
B"ENV"
C"CI"
D"JENKINS"
Attempts:
3 left
💡 Hint
Common Mistakes
Using assumeTrue instead of assumeFalse here.
Using wrong environment variable name.
Not using quotes around the environment variable name.
3fill in blank
hard

Fix the error in the assumption to skip the test if the user is not admin.

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

@Test
void testAdminOnly() {
    assumeTrue(System.getProperty("user.role").equals([1]));
    // test code here
}
Drag options to blanks, or click blank then click option'
A"user"
Badmin
Ctrue
D"admin"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted admin causing syntax error.
Using boolean true instead of string comparison.
Comparing to "user" instead of "admin".
4fill in blank
hard

Fill both blanks to skip the test if the Java version is less than 11.

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

@Test
void testJava11OrHigher() {
    String version = System.getProperty("java.version");
    assumeTrue(Integer.parseInt(version.split("\\.")[[1]]) [2] 11);
    // 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 for version.
Using < instead of >= operator.
Not parsing the version string to integer.
5fill in blank
hard

Fill all three blanks to skip the test if the environment is not "dev" or the user is not "tester".

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

@Test
void testDevTesterOnly() {
    String env = System.getenv([1]);
    String user = System.getProperty([2]);
    assumeTrue(env.equals([3]) && user.equals("tester"));
    // test code here
}
Drag options to blanks, or click blank then click option'
A"ENVIRONMENT"
B"user.name"
C"dev"
D"production"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong environment variable or property names.
Using "production" instead of "dev".
Not quoting string literals.