Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to use assumingThat for a conditional assertion.
JUnit
import static org.junit.jupiter.api.Assumptions.assumingThat; import static org.junit.jupiter.api.Assertions.assertEquals; @Test void testConditional() { String env = "DEV"; assumingThat(env.equals("[1]"), () -> { assertEquals(5, 2 + 3); }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different environment string that does not match the env variable.
Forgetting to use assumingThat and using assertEquals directly.
✗ Incorrect
The assumingThat method runs the assertion only if the condition is true. Here, the condition checks if env equals "DEV".
2fill in blank
mediumComplete the code to skip the assertion when the condition is false using assumingThat.
JUnit
import static org.junit.jupiter.api.Assumptions.assumingThat; import static org.junit.jupiter.api.Assertions.assertTrue; @Test void testSkipAssertion() { boolean isFeatureEnabled = false; assumingThat(isFeatureEnabled, () -> { assertTrue([1]); }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using false inside assertTrue which causes failure when the assertion runs.
Using the condition variable inside assertTrue which is redundant.
✗ Incorrect
The assertion inside assumingThat runs only if isFeatureEnabled is true. The assertion checks true to pass.
3fill in blank
hardFix the error in the code by completing the assumingThat condition correctly.
JUnit
import static org.junit.jupiter.api.Assumptions.assumingThat; import static org.junit.jupiter.api.Assertions.assertEquals; @Test void testFixCondition() { String mode = "PROD"; assumingThat([1], () -> { assertEquals(10, 5 + 5); }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == to compare strings which checks reference equality, not content.
Using equalsIgnoreCase when exact match is required.
✗ Incorrect
In Java, string equality should be checked with equals(), not ==. So mode.equals("PROD") is correct.
4fill in blank
hardFill both blanks to correctly use assumingThat with a boolean condition and an assertion.
JUnit
import static org.junit.jupiter.api.Assumptions.assumingThat; import static org.junit.jupiter.api.Assertions.assertFalse; @Test void testBothBlanks() { boolean isActive = [1]; assumingThat(isActive, () -> { assertFalse([2]); }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isActive to false which skips the assertion.
Using assertFalse(true) which fails the test.
✗ Incorrect
The condition isActive is true, so the assertion runs. The assertion checks assertFalse(false), which passes.
5fill in blank
hardFill all three blanks to write a conditional assertion using assumingThat with a string check and an assertion.
JUnit
import static org.junit.jupiter.api.Assumptions.assumingThat; import static org.junit.jupiter.api.Assertions.assertNotNull; @Test void testThreeBlanks() { String status = [1]; assumingThat(status.equals([2]), () -> { assertNotNull([3]); }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable status for the first blank, causing a compilation error.
Using a wrong string for the condition check.
Passing null to assertNotNull causing failure.
✗ Incorrect
status is a string variable. The condition checks if status equals "ACTIVE". The assertion checks that status is not null.