0
0
JUnittesting~20 mins

assumingThat for conditional assertions in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit AssumingThat Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of assumingThat with true condition
What is the output of the following JUnit test snippet when the condition is true?
JUnit
import static org.junit.jupiter.api.Assumptions.assumingThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

boolean isFeatureEnabled = true;

assumingThat(isFeatureEnabled, () -> {
    assertEquals(5, 2 + 3);
});

System.out.println("Test completed");
ATest completed (test fails with AssertionError)
BTest completed (test passes)
CTest skipped due to failed assumption
DCompilation error due to syntax
Attempts:
2 left
💡 Hint
Think about what assumingThat does when the condition is true.
Predict Output
intermediate
2:00remaining
Output of assumingThat with false condition
What happens when the condition in assumingThat is false in this JUnit snippet?
JUnit
import static org.junit.jupiter.api.Assumptions.assumingThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

boolean isFeatureEnabled = false;

assumingThat(isFeatureEnabled, () -> {
    assertEquals(5, 2 + 2);
});

System.out.println("Test completed");
ATest completed (test passes)
BTest completed (test fails with AssertionError)
CRuntime exception due to null pointer
DTest skipped due to failed assumption
Attempts:
2 left
💡 Hint
What does assumingThat do when the condition is false?
assertion
advanced
2:00remaining
Correct use of assumingThat for environment-based test
You want to run an assertion only if the environment variable "ENV" equals "prod". Which of the following JUnit code snippets correctly uses assumingThat for this?
AassumingThat(System.getenv("ENV") != null, () -> assertEquals(10, calculate()));
BassumingThat(System.getenv("ENV") == "prod", () -> assertEquals(10, calculate()));
CassumingThat(System.getenv("ENV").equalsIgnoreCase("prod"), () -> assertEquals(10, calculate()));
DassumingThat(System.getenv("ENV").equals("prod"), () -> assertEquals(10, calculate()));
Attempts:
2 left
💡 Hint
Use .equals to compare strings in Java, not ==.
🔧 Debug
advanced
2:00remaining
Why does this assumingThat assertion fail unexpectedly?
Consider this JUnit code snippet: boolean isReady = false; assumingThat(isReady = true, () -> assertEquals(1, 2)); Why does the assertion run and fail even though isReady was false initially?
JUnit
boolean isReady = false;
assumingThat(isReady = true, () -> assertEquals(1, 2));
ABecause the assignment operator '=' sets isReady to true, so the condition is true and assertion runs
BBecause assumingThat ignores the condition and always runs the assertion
CBecause isReady was false, so the assertion should not run but a bug causes it to run
DBecause assertEquals(1, 2) is always executed regardless of assumingThat
Attempts:
2 left
💡 Hint
Check the difference between '=' and '==' in Java conditions.
🧠 Conceptual
expert
2:00remaining
Behavior of assumingThat with multiple assertions inside
In a JUnit test, you use assumingThat with a condition that is false. Inside the lambda, you have multiple assertions. What happens to those assertions during test execution?
AAll assertions run but failures are ignored
BOnly the first assertion runs and others are skipped
CNone of the assertions inside the lambda run; the entire block is skipped
DAssertions run but results are logged without failing the test
Attempts:
2 left
💡 Hint
Think about how assumingThat controls execution flow based on the condition.