Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assumeFalse instead of assumeTrue here.
Not using quotes around the string "Windows".
Using incorrect OS name string.
✗ Incorrect
The assumeTrue method skips the test if the condition is false. Here, the test runs only if the OS name starts with "Windows".
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The assumeFalse method skips the test if the condition is true. Here, the test is skipped if the environment variable "CI" is set.
3fill in blank
hardFix 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'
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".
✗ Incorrect
The assumeTrue condition must compare the user role string to "admin" with quotes. Without quotes, it causes a compile error.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Java version strings start with major version at index 0. To skip the test if the Java version is less than 11, assumeTrue that the major version >= 11.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong environment variable or property names.
Using "production" instead of "dev".
Not quoting string literals.
✗ Incorrect
We get environment variable "ENVIRONMENT" and system property "user.name". The test runs only if environment is "dev" and user is "tester".