Bird
Raised Fist0
JUnittesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of assumingThat in JUnit tests?
easy
A. To run assertions only if a specific condition is true
B. To always run assertions regardless of conditions
C. To skip all tests unconditionally
D. To replace all assertions with print statements

Solution

  1. Step 1: Understand the role of assumingThat

    assumingThat is used to run assertions only when a given condition is true, otherwise it skips them.
  2. Step 2: Compare options with this behavior

    Only "To run assertions only if a specific condition is true" correctly describes this conditional behavior. Options A, B, and C do not match the purpose.
  3. Final Answer:

    To run assertions only if a specific condition is true -> Option A
  4. Quick Check:

    Conditional assertions = run only if true [OK]
Hint: Remember: assumingThat runs assertions only when condition is true [OK]
Common Mistakes:
  • Thinking assumingThat always runs assertions
  • Confusing assumingThat with skipping all tests
  • Believing assumingThat replaces assertions
2. Which of the following is the correct syntax to use assumingThat in a JUnit 5 test?
easy
A. assumingThat(assertEquals(5, result), condition);
B. assumingThat(condition, assertEquals(5, result));
C. assumingThat(condition, () -> assertEquals(5, result));
D. assumingThat(() -> assertEquals(5, result), condition);

Solution

  1. Step 1: Recall the method signature of assumingThat

    It takes a boolean condition first, then a lambda expression with assertions.
  2. Step 2: Match options to this signature

    assumingThat(condition, () -> assertEquals(5, result)); matches: assumingThat(condition, () -> assertion). Others have wrong order or missing lambda.
  3. Final Answer:

    assumingThat(condition, () -> assertEquals(5, result)); -> Option C
  4. Quick Check:

    Condition first, then lambda [OK]
Hint: Syntax: assumingThat(condition, () -> assertion) [OK]
Common Mistakes:
  • Putting assertion before condition
  • Not using a lambda for the assertion
  • Passing assertion directly without lambda
3. Consider the following code snippet:
int value = 10;
assumingThat(value > 5, () -> assertTrue(value < 20));
assumingThat(value < 5, () -> assertTrue(value > 0));

What will be the result of running this test?
medium
A. Both assertions run and pass
B. First assertion runs and passes; second is skipped
C. First assertion fails; second runs and passes
D. Both assertions are skipped

Solution

  1. Step 1: Evaluate the first condition and assertion

    Condition: value > 5 is true (10 > 5). So assertion value < 20 runs and passes.
  2. Step 2: Evaluate the second condition and assertion

    Condition: value < 5 is false (10 < 5). So assertion value > 0 is skipped.
  3. Final Answer:

    First assertion runs and passes; second is skipped -> Option B
  4. Quick Check:

    True condition runs assertion, false skips [OK]
Hint: Only assertions with true conditions run [OK]
Common Mistakes:
  • Assuming all assertions run regardless of condition
  • Confusing which assertions are skipped
  • Thinking skipped assertions cause failure
4. Identify the error in this JUnit 5 test using assumingThat:
int x = 3;
x = 4;
assumingThat(() -> x > 0, () -> assertEquals(3, x));
medium
A. The variable x is not final or effectively final
B. The assertion lambda is missing parentheses
C. The condition should be a boolean, not a lambda
D. The assertEquals method is used incorrectly

Solution

  1. Step 1: Check the variable capture rules for lambdas

    Local variables used in lambdas must be final or effectively final. Here, x is reassigned (x = 4), so it is not effectively final.
  2. Step 2: Identify the error in the code

    This causes a compilation error because lambdas cannot capture non-effectively final variables.
  3. Final Answer:

    The variable x is not final or effectively final -> Option A
  4. Quick Check:

    Lambda locals must be effectively final [OK]
Hint: Lambdas capture effectively final locals only [OK]
Common Mistakes:
  • Modifying local variables used in lambdas
  • Not declaring local variables as final when needed
  • Ignoring Java lambda capture rules
5. You want to write a test that only checks if a list is empty when the environment variable ENV equals "test". Which code snippet correctly uses assumingThat to achieve this?
hard
A. assumingThat(System.getenv("ENV").equals("test"), assertTrue(list.isEmpty()));
B. assumingThat(() -> System.getenv("ENV").equals("test"), assertTrue(list.isEmpty()));
C. assumingThat(System.getenv("ENV") == "test", () -> assertTrue(list.isEmpty()));
D. assumingThat(System.getenv("ENV").equals("test"), () -> assertTrue(list.isEmpty()));

Solution

  1. Step 1: Check the condition type

    The condition must be a boolean expression, so System.getenv("ENV").equals("test") is correct.
  2. Step 2: Verify the assertion lambda

    The assertion must be inside a lambda: () -> assertTrue(list.isEmpty()).
  3. Step 3: Evaluate each option

    A matches both requirements. B uses lambda for condition and passes assertion without lambda. C uses == for string comparison (wrong). D passes assertion without lambda.
  4. Final Answer:

    assumingThat(System.getenv("ENV").equals("test"), () -> assertTrue(list.isEmpty())); -> Option D
  5. Quick Check:

    Boolean condition + assertion lambda = correct [OK]
Hint: Condition boolean, assertion inside lambda [OK]
Common Mistakes:
  • Using lambda for condition instead of boolean
  • Using == for string comparison
  • Passing assertion without lambda
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard