0
0
JUnittesting~20 mins

Why assumptions control test execution in JUnit - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Assumption Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How do assumptions affect test execution flow?

In JUnit, what happens when an assumption fails during test execution?

AThe test is skipped and marked as ignored without failing.
BThe test fails immediately with an assertion error.
CThe test continues running but logs a warning.
DThe test is retried automatically until the assumption passes.
Attempts:
2 left
💡 Hint

Think about what assumptions are used for in conditional test execution.

Predict Output
intermediate
2:00remaining
Output of test with failed assumption

What is the test result when the following JUnit test runs?

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

public class SampleTest {
    @Test
    void testOnlyOnWindows() {
        assumeTrue(System.getProperty("os.name").startsWith("Windows"));
        // test code here
    }
}
ATest passes regardless of OS.
BTest fails with AssertionError if OS is not Windows.
CTest is skipped if OS is not Windows.
DTest throws NullPointerException if OS property is missing.
Attempts:
2 left
💡 Hint

Check what assumeTrue does when the condition is false.

assertion
advanced
2:00remaining
Correct use of assumptions in JUnit

Which code snippet correctly uses assumptions to skip a test when a required environment variable is missing?

AassertTrue(System.getenv("API_KEY") != null, "API_KEY is required");
BassumeTrue(System.getenv("API_KEY") != null, "API_KEY is required");
CassumeFalse(System.getenv("API_KEY") == null, "API_KEY is required");
DassumeThat(System.getenv("API_KEY"), is(notNullValue()));
Attempts:
2 left
💡 Hint

Remember assumptions skip tests when conditions are false.

🔧 Debug
advanced
2:00remaining
Why does this assumption not skip the test?

Given the code below, why does the test fail instead of skipping when the assumption is false?

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

public class DebugTest {
    @Test
    void test() {
        if (!System.getProperty("user.name").equals("admin")) {
            assumeTrue(false, "Only admin can run this test");
        }
        assertTrue(true);
    }
}
AThe assumption is inside an if block that never runs, so it does not skip.
BThe test fails because assumeTrue is called with false inside an if, causing an error.
CassumeTrue(false) always fails the test instead of skipping.
DThe test fails because the assumption is not called unconditionally and the condition logic is wrong.
Attempts:
2 left
💡 Hint

Check the logic of the if condition and when assumeTrue is called.

framework
expert
2:00remaining
How assumptions integrate with JUnit 5 test lifecycle

Which statement best describes how assumptions affect the execution of lifecycle methods (@BeforeEach, @AfterEach) in JUnit 5?

AIf an assumption fails in @BeforeEach, the test and @AfterEach are skipped.
BIf an assumption fails in @BeforeEach, the test is skipped but @AfterEach still runs.
CAssumptions do not affect lifecycle methods; they only affect test methods.
DIf an assumption fails in @AfterEach, the test is marked as failed.
Attempts:
2 left
💡 Hint

Consider what happens when setup assumptions fail before the test runs.