0
0
JUnittesting~10 mins

assumingThat for conditional assertions 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 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'
ASTAGE
BDEV
CTEST
DPROD
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.
2fill in blank
medium

Complete 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'
Atrue
Bfalse
CisFeatureEnabled
D!isFeatureEnabled
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.
3fill in blank
hard

Fix 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'
Amode == "PROD"
Bmode.equalsIgnoreCase("prod")
Cmode.equals("PROD")
Dmode != null
Attempts:
3 left
💡 Hint
Common Mistakes
Using == to compare strings which checks reference equality, not content.
Using equalsIgnoreCase when exact match is required.
4fill in blank
hard

Fill 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'
Atrue
Bfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isActive to false which skips the assertion.
Using assertFalse(true) which fails the test.
5fill in blank
hard

Fill 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'
A"ACTIVE"
Cstatus
D"INACTIVE"
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.